mikeal / node.couchapp.js

Utility for writing couchapps.
Apache License 2.0
406 stars 78 forks source link

added couchapp.loadFiles() #13

Closed christiansmith closed 13 years ago

christiansmith commented 13 years ago

Thanks for creating this!

It's really convenient to see the main couchapp code in single file, rather than mapped into little files in lots of directories like the python couchapp. But there are definitely cases where we might want to use some module or another on the server side and loadAttachments() doesn't seem to cover it. This addition loads file contents from a given directory (recursively) into a js object that can be added to a design document and require()'d in lists, shows, etc. Just a quick sketch, but it's working well for me so far...

Example:

var couchapp = require('couchapp'); ddoc = { _id: '_design/app', ... } ddoc.lib = couchapp.loadFiles('./lib/')

Try it out ;-)

christiansmith commented 13 years ago

Just added the ability to preprocess file contents before adding to the ddoc. For example, you might want to write jade templates that render to mustache for additional rendering later.

In your project directory, add a ./templates dir and some jade templates:

!!!5
html
  head
    //- jade locals.title
    title!= title
  body
    //- mustache variable for server-side rendering
    {{ content }}

app.js:

var couchapp = require('couchapp')
  , jade = require('jade');

ddoc = {
    _id: '_design/app'
  , views: {}
  , lists: {}
  , shows: {}
}

module.exports = ddoc;

options = {
    operators: [
      function renderJade (content, options) {
        var compiler = jade.compile(content);
        return compiler(options.locals || {});
      }
    ]
  , locals: { title: 'Now we\'re cookin with gas!' }
}

ddoc.templates = couchapp.loadFiles('./templates', options);
ddoc.vendor = couchapp.loadFiles('./vendor');
mikeal commented 13 years ago

I actually use to have this exact function before I did a refactor for sync.

The main issue I have with it right now is that I don't see any code to support the sync command that watches this file tree.

The most important thing for me to do in the short term is get the sync command working for app.js changes, I don't want to take another feature that doesn't sync at this time.

If you want to add sync support, I can take the patch, or if you want to wait for me to finish up the work on app.js sync I can keep this in mind, merge it, and add the sync support myself.

christiansmith commented 13 years ago

Interesting. I'll have a look at it. Thanks for the reply.

max-mapper commented 13 years ago

this should be merged in even without sync - it's a great way to use commonjs in couchapps (which is important now that 1.1 is out)