javascript - Override Backbone.sync() at Model level to send extra params? -
to quite honest i'm stuck trying override backbone's sync() method model, have signature function in place, , gets triggered correctly, i don't know put in function body in order make default call delete with arguments. ie.
class master.models.member extends backbone.model urlroot: '/api/members/' sync: (method, model, options) -> params = _.clone options backbone.sync method, model, params
i call this:
...... remove: -> @model.destroy collective_id: the_id
my intention there, pass collective_id param see there server. though it's inside options hash sync() , clone it, it won't make server! how can send param server?
(as is, thing reaches server model's id)
thanks in advance!
when call .destroy(), .fetch() or .save() call model.sync calls backbone.sync. it's proxy function. intended provide nice hook modifying ajax behavior of single model or models extend model.
- solution 1: override global backbone.sync
json.stringify
, modifycontenttype
when you've specified data sent delete request.- pros: can call
model.destroy()
, optionally pass inoptions
parameter
- pros: can call
- solution 2 - override model.sync method.
- pros: override affects individual models. isolated changes.
- cons: models wish delete data need extend correct 'base model'
- solution 3 - don't override , explicitly call model.sync of
stringify
,contenttype
stuff.- pros: isolated changes, won't affect other models. useful if you're integrating large codebase.
[solution 1] - global override of backbone.sync (this affect models)
javacript version
var oldbackbonesync = backbone.sync; backbone.sync = function( method, model, options ) { // delete request data if ( method === 'delete' && options.data ) { options.data = json.stringify(options.data); options.contenttype = 'application/json'; } // else, business usual. return oldbackbonesync.apply(this, [method, model, options]); }
usage:
var model, somemodel = backbone.model.extend({ /* urlroot, initialize, etc... */}); model = new somemodel(); model.destroy({ data: { /* data payload send delete request */ } });
[solution 2] - override backbone.destroy on base model , extend other models that.
override
// create own 'enhanced' model backbone.enhancedmodel = backbone.model.extend({ destroy: function( options ) { if ( options.data ) { // formats data back-end parse options.data = json.stringify(options.data); } // transform delete requests application/json options.contenttype = 'application/json'; backbone.model.prototype.destroy.call(this, options); } });
usage
var model, somemodel = backbone.enhancedmodel.extend({ /* urlroot, initialize, etc... */}) model = new somemodel(); somemodel.destroy({ data: { /* additional data payload */ } });
[solution 3] - call .destroy() correct parameters.
if sending data destroy requests isolated thing, adequate solution.
when calling model.destroy()
pass in data
, contenttype
options so:
javascript version/usage
var additionaldata = { collective_id: 14 }; model.destroy({ data: json.stringify(additionaldata), contenttype: 'application/json' });
the "problem" (with backbone, not solutions):
backbone.js makes assumption (view source) delete requests do not have data payload.
// delete methods excluded having data processed , contenttype altered. if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) { params.contenttype = 'application/json'; params.data = json.stringify(options.attrs || model.tojson(options)); }
in assumed restful api call, data required id should appended urlroot
property.
var bookmodel = backbone.model.extend({ urlroot: 'api/book' }); var book1 = new bookmodel({ id: 1 }); book1.destroy()
the delete request sent like
delete => api/book/1 contenttype: content-type:application/x-www-form-urlencoded; charset=utf-8
Comments
Post a Comment