diff --git a/README.markdown b/README.markdown index 672f106..7f0f376 100644 --- a/README.markdown +++ b/README.markdown @@ -54,11 +54,11 @@ var Dog = endtable.Object.extend( engine: engine, type: 'dog', customViews: [ - function lowerName(doc) { + function lowerName(doc) { if(doc.type=='dog') emit(doc.name.toLowerCase(),doc); }, - function otherNamedView(doc) { ... } + function otherNamedView(doc) { ... } ] } ); diff --git a/lib/couch-connector.js b/lib/couch-connector.js index 015314e..1527cae 100755 --- a/lib/couch-connector.js +++ b/lib/couch-connector.js @@ -104,16 +104,17 @@ exports.connector = Class.extend({ type: '', keys: [] } + params = arrayHelpers.extend(defaults, {}, params); - - // if there's a custom view specified, load by that instead - var viewURL; - if( params.customView ) { - viewURL = this.buildCustomViewURL(params.customView,params.type); - } - else { - viewURL = this.buildViewURL(params.keys, params.type); + + // if there's a custom view specified, load by that instead + var viewURL; + if( params.customView ) { + viewURL = this.buildCustomViewURL(params.customView, params.type); + } else { + viewURL = this.buildViewURL(params.keys, params.type); } + var viewParams = this.buildViewParams({ key: params.key, startkey: params.startkey, @@ -147,24 +148,23 @@ exports.connector = Class.extend({ return '/_design/' + type + '/_view/' + this.buildViewName(keys); } }, - - buildCustomViewURL: function(name, type) { + + buildCustomViewURL: function(name, type) { if (this.legacy) { return '/_view/' + type + '/' + this.buildCustomViewName(name); } else { return '/_design/' + type + '/_view/' + this.buildCustomViewName(name); } - }, + }, buildDocumentName: function(type) { return '_design/' + type; }, - buildCustomViewName: function(name) { - return 'custom_'+name; - }, + buildCustomViewName: function(name) { + return 'custom_'+name; + }, - buildViewName: function(keys) { var viewName = 'by'; @@ -176,15 +176,16 @@ exports.connector = Class.extend({ }, buildViewParams: function(getParams) { - var i = 0; - var getParamsString = ''; + var i = 0, + getParamsString = ''; + for (var key in getParams) { if (getParams.hasOwnProperty(key) && getParams[key]) { - - // we omit the custom view keys, if specified - if(key=='customView') { - continue; - } + // we omit the custom view keys, if specified + if(key=='customView') { + continue; + } + if (i) { getParamsString += '&'; } @@ -218,17 +219,17 @@ exports.connector = Class.extend({ if (!doc) { doc = {views: {}}; } - - doc.views[_this.buildViewName(params.keys)] = { - map: 'function(doc) { if (doc.type == \'' + params.type + '\') {' + _this.buildEmitKey(params.keys) + '} }' - }; + + doc.views[_this.buildViewName(params.keys)] = { + map: 'function(doc) { if (doc.type == \'' + params.type + '\') {' + _this.buildEmitKey(params.keys) + '} }' + }; _this.connection.saveDoc( documentName, doc, function(error, doc) { callback(error, doc); - _this.synchronousProcessor.next(); + _this.synchronousProcessor.next(); } ); }); @@ -236,47 +237,45 @@ exports.connector = Class.extend({ }, [documentName, callback]); }, - createCustomView: function(params,callback) { - callback = this.errorHandler.wrapCallback( + createCustomView: function(params,callback) { + var _this = this; + + callback = this.errorHandler.wrapCallback( callback, 'CouchConnector', 'createCustomView', params ); - if(!params.customView || typeof(params.customView) != 'function' - || !params.customView.name || params.customView.name =='') { - callback({message:'params.customView must be a named function'},null); - } - - var _this = this; + if(!params.customView || typeof(params.customView) != 'function' || !params.customView.name) { + callback({message:'params.customView must be a named function'},null); + } - var documentName = this.buildDocumentName(params.type); + var documentName = this.buildDocumentName(params.type); + this.synchronousProcessor.runSynchronous(_this, function(documentName, callback) { _this.connection.getDoc(documentName, function(error, doc) { if (!doc) { doc = {views: {}}; } - - // create custom view - doc.views[_this.buildCustomViewName(params.customView.name)] = { - map: params.customView.toString().replace(/function.*?\(/,'function(') - }; + + // create custom view + doc.views[_this.buildCustomViewName(params.customView.name)] = { + map: params.customView.toString().replace(/function.*?\(/,'function(') + }; _this.connection.saveDoc( documentName, doc, function(error, doc) { callback(error, doc); - _this.synchronousProcessor.next(); + _this.synchronousProcessor.next(); } ); }); - }, [documentName, callback]); - - }, + }, buildEmitKey: function(keys) { var emitKey = 'emit('; diff --git a/lib/endtable-object.js b/lib/endtable-object.js index 8149cac..ae77eb6 100755 --- a/lib/endtable-object.js +++ b/lib/endtable-object.js @@ -23,19 +23,21 @@ exports.Object = MonitoredObject.extend({ this.firstSaveCallback = firstSaveCallback; this.synchronousProcessor = new SynchronousProcessor(); this.Constructor = params.Constructor || exports.Object; - - // now create the custom views, if passed in - if(this.customViews && this.customViews.length > 0) { - for(var i=0;i 0, prefix + ' custom map function not properly created.'); - finished(); - }, - - 'should raise an error when createCustomView is called with param customView:myFn that is not a named function': function(finished, prefix) { - var c = createMockConnection(); - - var custom_view = function(doc) { - if(doc.type == 'custom_view_test') { - emit( toLower(doc.id+'_CUSTOM_TEST'), doc); - } - }; - - c.createCustomView({ - customView:custom_view, - type:'custom_view_test' - }, function(error,doc) { - equal(true,error.message == 'params.customView must be a named function',prefix + 'setting customView to an anonymous function did not raise an error in createCustomView'); - finished(); - }); - }, - - 'should raise an error when createCustomView is called with param custom_view with value that is not a function': function(finished, prefix) { - var c = createMockConnection(); - - c.createCustomView({ - customView:'not a function', - type:'custom_view_test' - }, function(error,doc) { - equal(true,error.message == 'params.customView must be a named function',prefix + 'setting custom_view to something other than a function did not raise an error in createCustomView'); - finished(); - }); - } + + 'should create the specified map function myFn when createCustomView is called with params customView:myFn': function(finished, prefix) { + var c = createMockConnection(); + + var customView = function custom(doc) { + if(doc.type == 'custom_view_test') { + emit( toLower(doc.id+'_CUSTOM_TEST'), doc); + } + }; + + var cv_name = customView.name; + + c.createCustomView({ + customView:customView, + type:'custom_view_test' + }); + + equal(true, lastDocument.views[c.buildCustomViewName(cv_name)].map.indexOf('_CUSTOM_TEST') > 0, prefix + ' custom map function not properly created.'); + finished(); + }, + + 'should raise an error when createCustomView is called with param customView:myFn that is not a named function': function(finished, prefix) { + var c = createMockConnection(); + + var custom_view = function(doc) { + if(doc.type == 'custom_view_test') { + emit( toLower(doc.id+'_CUSTOM_TEST'), doc); + } + }; + + c.createCustomView({ + customView:custom_view, + type:'custom_view_test' + }, function(error,doc) { + equal(true,error.message == 'params.customView must be a named function',prefix + 'setting customView to an anonymous function did not raise an error in createCustomView'); + finished(); + }); + }, + + 'should raise an error when createCustomView is called with param custom_view with value that is not a function': function(finished, prefix) { + var c = createMockConnection(); + + var cv_name = 'lower_docid'; + + c.createCustomView({ + customView:'not a function', + type:'custom_view_test' + }, function(error,doc) { + equal(true,error.message == 'params.customView must be a named function',prefix + 'setting custom_view to something other than a function did not raise an error in createCustomView'); + finished(); + }); + } };