Using require() or // !json, !code in CouchDB?
In the CouchDB Definitive Guide the author says you could use:
// !json templates.edit
// !json blog
// !code vendor/couchapp/path.js
// !code vendor/couchapp/template.js
But why use those macros when you can use require()?
A开发者_如何学Cre there occasions I want to use the macros instead?
This was a feature from before CommonJS modules were introduced into CouchDB. These macros are pretty much obsolete now.
Basically this is only of interest when using CouchDB with CouchApp.
It allows for easier separation of the data from the show function.
Normally CouchDB uses the Mozilla's SpiderMonkey JavaScript Engine to render the views, therefore there is not CommonJS implementation available.
Basically CouchApp allows you to use those JavaScript "show" function (which are stored in the DB) for rendering out HTML.
So in terms of Node.js, no it doesn't really make sense to use the macros. Also, I quickly looked through the soruce of two of the available CouchDB modules for Node.js, and I couldn't find any support for the macros, which further indicates that this is some CouchApp specific functionality.
Just to add a couple of related things which I have found useful as a newcomer to CouchDB.
Firstly the require()
function doesn't seem to be able to include design document attachments, meaning that if you want to include the same script both on the client and server you cannot put them in attachments as you might expect.
That is, although the following works OK if the design document has a property somewhere.foo
containing Javascript:
var js = require('somewhere/foo')
This doesn't:
var js = require('_attachments/foo')
The error is (I'm using CouchDB v1.6):
{"error":"invalid_require_path",
"reason":"Must require a JavaScript string, not: object"}
Because of this I have been putting my scripts in the design document as properties, not attachments. This means they can be accessed on the server via require()
, and on the client too (via a show function).
Secondly, only require()
ing Javascript is possible, which means you can't use it to include (say) HTML templates uploaded as design document properties.
The !code
and !json
directives supported by couchapp don't help much here. (Not to mention that I don't seem to have much success getting them to work at all...)
The trick I found useful for templates is that you can access design document properties directly in shows and views using this
. So given a design document property templates.edit
, this works:
var template = this.templates.edit;
This ties in nicely, as it can also be used to provide a show function which delivers Javascript which you have embedded as a property. Say this is in the design doc's shows.myscript
property:
function(doc, req) {
return this.js.myscript;
}
Then the URL http://localhost:5984/<database>/_design/<id>/_show/myscript
will serve it to the client, meaning you can refer to it in your HTML. You can also use your show function to concatenate and minimise several scripts embedded in this way into one package, if you wish.
Currently they are still both in use. While using require
seems cleaner,it is harder to debug because dependencies are evaluated at runtime. Using the macros, code is looked up before pushing to Couch
精彩评论