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); | |
| } | |
| } | |
| }); | |
| }; |