Tuesday, 15 May 2012

javascript - How do I modify the URL generated during Backbone sync, but only for a specific requests -



javascript - How do I modify the URL generated during Backbone sync, but only for a specific requests -

i alter url generated when entity calls destroy. instead of writing http delete /{action}/{entityid}, send /{action}/{entityid}/{secondentityid}.

item.destroy({ data: $.param({ playlistid: playlistid }), processdata: true, success: callback, error: function (error) { console.error(error); } });

i thought might work, doesn't seem append on additional parameters. have implement own sync method in entirety if want extend destroys' url?

you can override through passing in .url property in options when phone call destroy. since assume you'd want every single call, can this:

var mymodel = backbone.model.extend({ destroy: function(options) { // override url options || (options = {}); // can set whatever need here, options.url = 'http://www.awesome.com/destroy/' + this.get('id') + '/' + this.get('secondaryid'); // phone call model.destroy(). // reusing existing functionality backbone.model.destroy(). backbone.model.prototype.destroy.apply(this, arguments); } }); var m= new mymodel({ id: 123, secondaryid: 456 }); // note: need set 'id' in order destroy() phone call successful. m.destroy({ sucess: function() { console.log('good'); }, error: function() { console.log('bad'); } });

if open firebug or chrome dev tools, should see xhr/ajax phone call made www.awesome.com.

since mentioned want across entities have, can in case create basemodel in application, , have entities extend it.

anyway, hope helps!

jsfiddle this: http://jsfiddle.net/ewqad/

javascript backbone.js

No comments:

Post a Comment