Skip to content

Instantly share code, notes, and snippets.

@ErichBSchulz
Created May 27, 2013 11:57
Show Gist options
  • Save ErichBSchulz/5656692 to your computer and use it in GitHub Desktop.
Save ErichBSchulz/5656692 to your computer and use it in GitHub Desktop.
Bridge between backbonejs models (and collections) and the CiviCRM CRM.api
var CRMModel = Backbone.Model.extend({
// redirect sync to custom method
sync: this.crmSync,
// Custom bridge to V3 civicrm API
crmSync: function(method, model, options) {
// make a shallow copy of options ? uneccesary but seems safest??
var params = _.clone(options);
// look up this models enity (as either method or property)
var entity = _.result(this, 'entity');
var action = ''; // is there a good value to pass the api to throw error?
switch (method) {
case "create": // create the model on the server
action = 'create'; // CHECKME!
break;
case "read": // read this model from the server and return it
action = 'read'; // CHECKME!
break;
case "update": // update the model on the server with the argument
action = 'update'; // CHECKME!
break;
case "delete": // delete the model from the server.
action = 'delete'; // CHECKME!
break;
}
// creat success handler:
var successHandler = function(model) {
// stuff in here??
if(options.success) options.success(model);
};
// creat success handler:
// FIXME this needs to be called somehow
var errorHandler = function(model) {
// stuff in here??
if(options.error) options.error(model);
};
// call API
CRM.api(entity, action, params, {success: successHandler})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment