Created
May 27, 2013 11:57
-
-
Save ErichBSchulz/5656692 to your computer and use it in GitHub Desktop.
Bridge between backbonejs models (and collections) and the CiviCRM CRM.api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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