X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=js%2Fcrm.backbone.js;h=e384466213b1cae524f0b7f3939cea3b60acc4fb;hb=43e5f0f62d03d30d5b9d54ff2c87f1f7e507a3cf;hp=87c1c5cbc3f0aa0df8e8ce680c570aa72e0522ea;hpb=a765d024b0c845ee1233b1811277a9e8ab7ba01a;p=civicrm-core.git diff --git a/js/crm.backbone.js b/js/crm.backbone.js index 87c1c5cbc3..e384466213 100644 --- a/js/crm.backbone.js +++ b/js/crm.backbone.js @@ -1,5 +1,4 @@ -(function($) { - var CRM = (window.CRM) ? (window.CRM) : (window.CRM = {}); +(function($, _) { if (!CRM.Backbone) CRM.Backbone = {}; /** @@ -16,8 +15,9 @@ CRM.Backbone.sync = function(method, model, options) { var isCollection = _.isArray(model.models); + var apiOptions, params; if (isCollection) { - var apiOptions = { + apiOptions = { success: function(data) { // unwrap data options.success(_.toArray(data.values)); @@ -32,14 +32,14 @@ }; switch (method) { case 'read': - CRM.api(model.crmEntityName, 'get', model.toCrmCriteria(), apiOptions); + CRM.api(model.crmEntityName, model.toCrmAction('get'), model.toCrmCriteria(), apiOptions); break; // replace all entities matching "x.crmCriteria" with new entities in "x.models" case 'crm-replace': - var params = this.toCrmCriteria(); + params = this.toCrmCriteria(); params.version = 3; params.values = this.toJSON(); - CRM.api(model.crmEntityName, 'replace', params, apiOptions); + CRM.api(model.crmEntityName, model.toCrmAction('replace'), params, apiOptions); break; default: apiOptions.error({is_error: 1, error_message: "CRM.Backbone.sync(" + method + ") not implemented for collections"}); @@ -47,10 +47,10 @@ } } else { // callback options to pass to CRM.api - var apiOptions = { + apiOptions = { success: function(data) { // unwrap data - var values = _.toArray(data['values']); + var values = _.toArray(data.values); if (data.count == 1) { options.success(values[0]); } else { @@ -70,24 +70,24 @@ switch (method) { case 'create': // pass-through case 'update': - var params = model.toJSON(); - params.options || (params.options = {}); + params = model.toJSON(); + if (!params.options) params.options = {}; params.options.reload = 1; if (!model._isDuplicate) { - CRM.api(model.crmEntityName, 'create', params, apiOptions); + CRM.api(model.crmEntityName, model.toCrmAction('create'), params, apiOptions); } else { - CRM.api(model.crmEntityName, 'duplicate', params, apiOptions); + CRM.api(model.crmEntityName, model.toCrmAction('duplicate'), params, apiOptions); } break; case 'read': case 'delete': var apiAction = (method == 'delete') ? 'delete' : 'get'; - var params = model.toCrmCriteria(); + params = model.toCrmCriteria(); if (!params.id) { apiOptions.error({is_error: 1, error_message: 'Missing ID for ' + model.crmEntityName}); return; } - CRM.api(model.crmEntityName, apiAction, params, apiOptions); + CRM.api(model.crmEntityName, model.toCrmAction(apiAction), params, apiOptions); break; default: apiOptions.error({is_error: 1, error_message: "CRM.Backbone.sync(" + method + ") not implemented for models"}); @@ -116,8 +116,17 @@ // Defaults - if specified in ModelClass, preserve _.defaults(ModelClass.prototype, { crmEntityName: crmEntityName, + crmActions: {}, // map: string backboneActionName => string serverSideActionName + crmReturn: null, // array: list of fields to return + toCrmAction: function(action) { + return this.crmActions[action] ? this.crmActions[action] : action; + }, toCrmCriteria: function() { - return (this.get('id')) ? {id: this.get('id')} : {}; + var result = (this.get('id')) ? {id: this.get('id')} : {}; + if (!_.isEmpty(this.crmReturn)) { + result.return = this.crmReturn; + } + return result; }, duplicate: function() { var newModel = new ModelClass(this.toJSON()); @@ -308,8 +317,18 @@ // Defaults - if specified in CollectionClass, preserve _.defaults(CollectionClass.prototype, { crmEntityName: CollectionClass.prototype.model.prototype.crmEntityName, + crmActions: {}, // map: string backboneActionName => string serverSideActionName + toCrmAction: function(action) { + return this.crmActions[action] ? this.crmActions[action] : action; + }, toCrmCriteria: function() { - return (this.crmCriteria) ? _.extend({}, this.crmCriteria) : {}; + var result = (this.crmCriteria) ? _.extend({}, this.crmCriteria) : {}; + if (!_.isEmpty(this.crmReturn)) { + result.return = this.crmReturn; + } else if (this.model && !_.isEmpty(this.model.prototype.crmReturn)) { + result.return = this.model.prototype.crmReturn; + } + return result; }, /** @@ -330,7 +349,7 @@ debouncedFetch: _.debounce(function() { this.fetch({reset: true}); - }, 500), + }, 100), /** * Reconcile the server's collection with the client's collection. @@ -341,7 +360,7 @@ * @param Object options - accepts "success" and "error" callbacks */ save: function(options) { - options || (options = {}); + if (!options) options = {}; var collection = this; var success = options.success; options.success = function(resp) { @@ -352,19 +371,22 @@ }; wrapError(collection, options); - return this.sync('crm-replace', this, options) + return this.sync('crm-replace', this, options); } }); // Overrides - if specified in CollectionClass, replace _.extend(CollectionClass.prototype, { sync: CRM.Backbone.sync, initialize: function(models, options) { - options || (options = {}); + if (!options) options = {}; if (options.crmCriteriaModel) { this.setCriteriaModel(options.crmCriteriaModel); } else if (options.crmCriteria) { this.crmCriteria = options.crmCriteria; } + if (options.crmActions) { + this.crmActions = _.extend(this.crmActions, options.crmActions); + } if (origInit) { return origInit.apply(this, arguments); } @@ -395,13 +417,13 @@ * - error: function(collection, error) */ CRM.Backbone.findCreate = function(options) { - options || (options = {}); + if (!options) options = {}; var collection = new options.CollectionClass([], { crmCriteria: options.crmCriteria }); collection.fetch({ success: function(collection) { - if (collection.length == 0) { + if (collection.length === 0) { var attrs = _.extend({}, collection.crmCriteria, options.defaults || {}); var model = collection._prepareModel(attrs, options); options.success(model); @@ -543,8 +565,8 @@ var wrapError = function (model, options) { var error = options.error; options.error = function(resp) { - if (error) error(model, resp, options); + if (error) error(model, resp, optio); model.trigger('error', model, resp, options); }; }; -})(cj); +})(CRM.$, CRM._);