You need to be added to the ContributorsGroup to edit the wiki. But don't worry! Just email any Mailing List or grab us on IRC and let us know your user name. |
Javascript Pattern to Share Code Between View and Other Functions
This works only if one is using CouchApp since this pattern is dependent on CouchApp directives.
CouchApp directives are explained here: Chris Strom's blog.
Shared Code
The following Javascript code fragment can be used as an example of a shared library between a View Map function and other functions (in this example, a validate_update_doc function). In the design document, create an entry under vendor.myComp.utils which is:
1 var utils = {
2
3 isArray: function(o) {
4 if( typeof(o) !== 'object' ) return false;
5 if( typeof(o.length) !== 'number' ) return false;
6 if( typeof(o.push) !== 'function' ) return false;
7 if( typeof(o.pop) !== 'function' ) return false;
8 if( typeof(o.concat) !== 'function' ) return false;
9 if( typeof(o.join) !== 'function' ) return false;
10 if( typeof(o.slice) !== 'function' ) return false;
11 if( typeof(o.reverse) !== 'function' ) return false;
12 if( typeof(o.splice) !== 'function' ) return false;
13 if( typeof(o.sort) !== 'function' ) return false;
14
15 return true;
16 }
17 };
18
19 // CommonJS bindings
20 if( typeof(exports) === 'object' ) {
21 exports.isArray = utils.isArray;
22 };
Notes:
In a couchApp (an application developed and deployed using CouchApp), the content above would be saved in a file named "vendor/myComp/utils.js".
Files uploaded using CouchApp are stripped of their extensions since it would lead to confusion as the dot (period) is the natural Javascript separator. Therefore, the shared code is "known" by CouchApp as "vendor/myComp/utils.js" while it is referred internally to the design documents as "vendor.myComp.utils".
Inclusion using CommonJs
Using CommonJs, the library defined above (shared code) would be included in a "validate_update_doc" function as follows:
Notes:
The 'require' function in CommonJs exposes the 'exports' defined in the library and assigns them to the variable.
- The variable 'myUtils' can be used to access the definitions from the shared code.
Note that the path uses slashes and that the file extension (if using CouchApp) is omitted.
Inclusion using CouchApp directives
Since View Map and View Reduce functions do not have access to CommonJs, the way one can include shared code within those functions is to use CouchApp directives. More specifically, in CouchApp, the 'code' directive instructs the tool to include the content of another file (verbatim) at the location of the directive before uploading the file to CouchDb.
An example of View Map function that shares the code defined above:
Since CouchApp includes the whole file at the point of the directive, the resulting content uploaded for this map function is:
1 function(doc) {
2
3 var utils = {
4
5 isArray: function(o) {
6 if( typeof(o) !== 'object' ) return false;
7 if( typeof(o.length) !== 'number' ) return false;
8 if( typeof(o.push) !== 'function' ) return false;
9 if( typeof(o.pop) !== 'function' ) return false;
10 if( typeof(o.concat) !== 'function' ) return false;
11 if( typeof(o.join) !== 'function' ) return false;
12 if( typeof(o.slice) !== 'function' ) return false;
13 if( typeof(o.reverse) !== 'function' ) return false;
14 if( typeof(o.splice) !== 'function' ) return false;
15 if( typeof(o.sort) !== 'function' ) return false;
16
17 return true;
18 }
19 };
20
21 // CommonJS bindings
22 if( typeof(exports) === 'object' ) {
23 exports.isArray = utils.isArray;
24 };
25
26 // Verify that list is an array
27 if( utils.isArray(doc.list) ) {
28 for(var i=0,e=doc.list.length; i<e; ++i) {
29 emit(doc.list[i],doc);
30 }
31 }
32 }
Notes: