You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by Apache Wiki <wi...@apache.org> on 2010/12/01 19:49:58 UTC

[Couchdb Wiki] Update of "JavascriptPatternViewCommonJs" by jpfiset

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The "JavascriptPatternViewCommonJs" page has been changed by jpfiset.
http://wiki.apache.org/couchdb/JavascriptPatternViewCommonJs

--------------------------------------------------

New page:
= 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: [[http://japhr.blogspot.com/2010/02/couchapp-templates-for-showing.html|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:

{{{#!highlight javascript
var utils = {

   isArray: function(o) {
      if( typeof(o) !== 'object' ) return false;
      if( typeof(o.length) !== 'number' ) return false;
      if( typeof(o.push) !== 'function' ) return false;
      if( typeof(o.pop) !== 'function' ) return false;
      if( typeof(o.concat) !== 'function' ) return false;
      if( typeof(o.join) !== 'function' ) return false;
      if( typeof(o.slice) !== 'function' ) return false;
      if( typeof(o.reverse) !== 'function' ) return false;
      if( typeof(o.splice) !== 'function' ) return false;
      if( typeof(o.sort) !== 'function' ) return false;

      return true;
   }
};

// CommonJS bindings
if( typeof(exports) === 'object' ) {
   exports.isArray = n2utils.isArray;
};
}}}

Notes:
  1. In a couchApp (an application developed and deployed using CouchApp), the content above would be saved in a file named "vendor/myComp/utils.js". 
  1. 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 is 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:

{{{#!highlight javascript
function(newDoc, oldDoc, userCtxt) {
	
   var myUtils = require('vendor/myComp/utils');
   
   // Verify geometries
   if( newDoc.type === 'myDoc' ) {

      // Verify that list is an array
      if( !myUtils.isArray(newDoc.list) ) {
         throw( {forbidden: 'Invalid or missing list'} );
      }
   }
}
}}}

Notes:
  1. The 'require' function in CommonJs exposes the 'exports' defined in the library and assigns them to the variable.
  1. The variable 'utils' can be used to access the definitions from the shared code.
  1. 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:

{{{#!highlight javascript
function(doc) {
	
   // !code vendor/myComp/utils.js
   
   // Verify that list is an array
   if( utils.isArray(doc.list) ) {
      for(var i=0,e=doc.list.length; i<e; ++i) {
         emit(doc.list[i],doc);
      }
   }
}
}}}

Since CouchApp includes the whole file at the point of the directive, the resulting content uploaded for this map function is:


{{{#!highlight javascript
function(doc) {
	
   var utils = {
   
      isArray: function(o) {
         if( typeof(o) !== 'object' ) return false;
         if( typeof(o.length) !== 'number' ) return false;
         if( typeof(o.push) !== 'function' ) return false;
         if( typeof(o.pop) !== 'function' ) return false;
         if( typeof(o.concat) !== 'function' ) return false;
         if( typeof(o.join) !== 'function' ) return false;
         if( typeof(o.slice) !== 'function' ) return false;
         if( typeof(o.reverse) !== 'function' ) return false;
         if( typeof(o.splice) !== 'function' ) return false;
         if( typeof(o.sort) !== 'function' ) return false;

         return true;
      }
   };

   // CommonJS bindings
   if( typeof(exports) === 'object' ) {
      exports.isArray = n2utils.isArray;
   };
   
   // Verify that list is an array
   if( utils.isArray(doc.list) ) {
      for(var i=0,e=doc.list.length; i<e; ++i) {
         emit(doc.list[i],doc);
      }
   }
}
}}}

Notes:
  1. In this case, the CommonJs bindings are ignored since 'exports' is not defined in a View Map function.
  1. The shared code is accessed via the variable named 'utils' defined in the external content file.
  1. The CouchApp directive requires the file extension.