Skip to content

Instantly share code, notes, and snippets.

@ifandelse
Created November 21, 2012 20:15
Show Gist options
  • Save ifandelse/4127371 to your computer and use it in GitHub Desktop.
Save ifandelse/4127371 to your computer and use it in GitHub Desktop.
Backbone.sync Override Using amplify.request

Backbone + Amplify.Request Experiment

Be warned - this code has not been tested yet....I just jotted it down stream-of-conscious style...

I will follow up with usage examples and verify that it really does work.

// We are using the model.url prop to identify the model 'type'. Example: 'customer' or 'orderItems'
// It *is* a little awkard to use the url prop for this, so you could easily use your own custom property
// like 'modelType' or whatever and bypass the need to use model.url altogether.
Backbone.sync = function(method, model, options) {
var modelData;
if(model instanceof Backbone.Model) {
modelData = model.toJSON();
} else {
// sometimes collections need to level metadata in their own model
// so if one is present, we get those attributes...
modelData = model.hasOwnProperty('model') ? model.model.toJSON() : {};
}
if (options.data) {
modelData = _.extend(modelData, options.data);
}
amplify.request({
resourceId: model.url + '-' + method
data: modelData,
success: function(result) {
if (options.success) {
options.success(result);
}
},
error: function(result) {
if (options.error) {
options.error(result);
}
}
});
};​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment