How do I DRY up my CouchDB views?

后端 未结 5 846
甜味超标
甜味超标 2020-12-06 00:17

What can I do to share code among views in CouchDB?

Example 1 -- utility methods

Jesse Hallett has some good utility methods, including

fun         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 01:06

    As per this blog post, you can add commonjs modules to the map function (but not the reduce function) in views in couchdb 1.1 by having a key called lib in your views object. A lot of popular javascript libraries like underscore.js adhere to the commonjs standard, so you can use them in your views by using require("views/lib/[your module name]").

    Say you include underscore.js as "underscore" in the lib object in views, like so:

    views: {
        lib: {
             underscore: "// Underscore.js 1.1.6\n ...
        }
        ...
        [ the rest of your views go here]
    }
    

    , you can then add the following to your view to get access to the _ module:

    var _ = require("views/lib/underscore");
    

    For custom libraries, all you need to do is make anything you want to share in your library a value to the global "exports" object.

提交回复
热议问题