Skip to content
Snippets Groups Projects
Commit 6bc90ee8 authored by totten's avatar totten
Browse files

crm.backbone.js - Allow collections to fetch records with alternative API actions

parent 17deafad
No related branches found
No related tags found
No related merge requests found
...@@ -32,14 +32,14 @@ ...@@ -32,14 +32,14 @@
}; };
switch (method) { switch (method) {
case 'read': case 'read':
CRM.api(model.crmEntityName, 'get', model.toCrmCriteria(), apiOptions); CRM.api(model.crmEntityName, model.toCrmAction('get'), model.toCrmCriteria(), apiOptions);
break; break;
// replace all entities matching "x.crmCriteria" with new entities in "x.models" // replace all entities matching "x.crmCriteria" with new entities in "x.models"
case 'crm-replace': case 'crm-replace':
var params = this.toCrmCriteria(); var params = this.toCrmCriteria();
params.version = 3; params.version = 3;
params.values = this.toJSON(); params.values = this.toJSON();
CRM.api(model.crmEntityName, 'replace', params, apiOptions); CRM.api(model.crmEntityName, model.toCrmAction('replace'), params, apiOptions);
break; break;
default: default:
apiOptions.error({is_error: 1, error_message: "CRM.Backbone.sync(" + method + ") not implemented for collections"}); apiOptions.error({is_error: 1, error_message: "CRM.Backbone.sync(" + method + ") not implemented for collections"});
...@@ -74,9 +74,9 @@ ...@@ -74,9 +74,9 @@
params.options || (params.options = {}); params.options || (params.options = {});
params.options.reload = 1; params.options.reload = 1;
if (!model._isDuplicate) { if (!model._isDuplicate) {
CRM.api(model.crmEntityName, 'create', params, apiOptions); CRM.api(model.crmEntityName, model.toCrmAction('create'), params, apiOptions);
} else { } else {
CRM.api(model.crmEntityName, 'duplicate', params, apiOptions); CRM.api(model.crmEntityName, model.toCrmAction('duplicate'), params, apiOptions);
} }
break; break;
case 'read': case 'read':
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
apiOptions.error({is_error: 1, error_message: 'Missing ID for ' + model.crmEntityName}); apiOptions.error({is_error: 1, error_message: 'Missing ID for ' + model.crmEntityName});
return; return;
} }
CRM.api(model.crmEntityName, apiAction, params, apiOptions); CRM.api(model.crmEntityName, model.toCrmAction(apiAction), params, apiOptions);
break; break;
default: default:
apiOptions.error({is_error: 1, error_message: "CRM.Backbone.sync(" + method + ") not implemented for models"}); apiOptions.error({is_error: 1, error_message: "CRM.Backbone.sync(" + method + ") not implemented for models"});
...@@ -116,6 +116,10 @@ ...@@ -116,6 +116,10 @@
// Defaults - if specified in ModelClass, preserve // Defaults - if specified in ModelClass, preserve
_.defaults(ModelClass.prototype, { _.defaults(ModelClass.prototype, {
crmEntityName: crmEntityName, crmEntityName: crmEntityName,
crmActions: {}, // map: string backboneActionName => string serverSideActionName
toCrmAction: function(action) {
return this.crmActions[action] ? this.crmActions[action] : action;
},
toCrmCriteria: function() { toCrmCriteria: function() {
return (this.get('id')) ? {id: this.get('id')} : {}; return (this.get('id')) ? {id: this.get('id')} : {};
}, },
...@@ -308,6 +312,10 @@ ...@@ -308,6 +312,10 @@
// Defaults - if specified in CollectionClass, preserve // Defaults - if specified in CollectionClass, preserve
_.defaults(CollectionClass.prototype, { _.defaults(CollectionClass.prototype, {
crmEntityName: CollectionClass.prototype.model.prototype.crmEntityName, 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() { toCrmCriteria: function() {
return (this.crmCriteria) ? _.extend({}, this.crmCriteria) : {}; return (this.crmCriteria) ? _.extend({}, this.crmCriteria) : {};
}, },
...@@ -365,6 +373,9 @@ ...@@ -365,6 +373,9 @@
} else if (options.crmCriteria) { } else if (options.crmCriteria) {
this.crmCriteria = options.crmCriteria; this.crmCriteria = options.crmCriteria;
} }
if (options.crmActions) {
this.crmActions = _.extend(this.crmActions, options.crmActions);
}
if (origInit) { if (origInit) {
return origInit.apply(this, arguments); return origInit.apply(this, arguments);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment