Return-Path: Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: (qmail 57393 invoked from network); 1 Dec 2010 18:50:20 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 1 Dec 2010 18:50:20 -0000 Received: (qmail 97816 invoked by uid 500); 1 Dec 2010 18:50:19 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 97782 invoked by uid 500); 1 Dec 2010 18:50:19 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 97775 invoked by uid 500); 1 Dec 2010 18:50:19 -0000 Delivered-To: apmail-incubator-couchdb-commits@incubator.apache.org Received: (qmail 97772 invoked by uid 99); 1 Dec 2010 18:50:19 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 01 Dec 2010 18:50:19 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.131] (HELO eos.apache.org) (140.211.11.131) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 01 Dec 2010 18:50:19 +0000 Received: from eosnew.apache.org (localhost [127.0.0.1]) by eos.apache.org (Postfix) with ESMTP id DD80CB77; Wed, 1 Dec 2010 18:49:58 +0000 (UTC) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Apache Wiki To: Apache Wiki Date: Wed, 01 Dec 2010 18:49:58 -0000 Message-ID: <20101201184958.93912.66202@eosnew.apache.org> Subject: =?utf-8?q?=5BCouchdb_Wiki=5D_Update_of_=22JavascriptPatternViewCommonJs?= =?utf-8?q?=22_by_jpfiset?= Dear Wiki user, You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for c= hange notification. The "JavascriptPatternViewCommonJs" page has been changed by jpfiset. http://wiki.apache.org/couchdb/JavascriptPatternViewCommonJs -------------------------------------------------- New page: =3D Javascript Pattern to Share Code Between View and Other Functions =3D 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]]. =3D=3D Shared Code =3D=3D The following Javascript code fragment can be used as an example of a share= d library between a View Map function and other functions (in this example,= a validate_update_doc function). In the design document, create an entry u= nder vendor.myComp.utils which is: {{{#!highlight javascript var utils =3D { isArray: function(o) { if( typeof(o) !=3D=3D 'object' ) return false; if( typeof(o.length) !=3D=3D 'number' ) return false; if( typeof(o.push) !=3D=3D 'function' ) return false; if( typeof(o.pop) !=3D=3D 'function' ) return false; if( typeof(o.concat) !=3D=3D 'function' ) return false; if( typeof(o.join) !=3D=3D 'function' ) return false; if( typeof(o.slice) !=3D=3D 'function' ) return false; if( typeof(o.reverse) !=3D=3D 'function' ) return false; if( typeof(o.splice) !=3D=3D 'function' ) return false; if( typeof(o.sort) !=3D=3D 'function' ) return false; return true; } }; // CommonJS bindings if( typeof(exports) =3D=3D=3D 'object' ) { exports.isArray =3D 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 i= t would lead to confusion as the dot (period) is the natural Javascript sep= arator. Therefore, the shared code is "known" by CouchApp as "vendor/myComp= /utils.js" while is is referred internally to the design documents as "ven= dor.myComp.utils". =3D=3D Inclusion using CommonJs =3D=3D Using CommonJs, the library defined above (shared code) would be included i= n a "validate_update_doc" function as follows: {{{#!highlight javascript function(newDoc, oldDoc, userCtxt) { = var myUtils =3D require('vendor/myComp/utils'); = // Verify geometries if( newDoc.type =3D=3D=3D '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 th= e library and assigns them to the variable. 1. The variable 'utils' can be used to access the definitions from the sh= ared code. 1. Note that the path uses slashes and that the file extension (if using = CouchApp) is omitted. = =3D=3D Inclusion using CouchApp directives =3D=3D Since View Map and View Reduce functions do not have access to CommonJs, th= e 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=3D0,e=3Ddoc.list.length; i