javascript - Adding functionality to Backbone Models? -


i'm trying figure out "correct" way of accomplishing custom update functions in backbone.js models. example of i'm trying is:

var cat = backbone.model.extend({    defaults: {     name     : 'mr. bigglesworth',     location : 'living room',     action   : 'sleeping'   },    sleep: function () {     // post /cats/{{ cat_id }}/action     // { action: "sleep" }   },    meow: function () {     // post /cats/{{ cat_id }}/action     // { action: "meow" }   }  }) 

from can tell, backbone.collection.save() method performs following:

post /cats/{{ cat_id }} { name: 'mr. bigglesworth', location: 'living room', action: '{{ value }} '} 

but api i'm working won't let me change action way, by:

post /cats/{{ cat_id }}/action { action: "{{ value }}" } 

hopefully makes sense?

any appreciated.

you can pass url parameter when call save. maybe can this:

var cat = backbone.model.extend({   urlroot: '/cats/',    defaults: {     name     : 'mr. bigglesworth',     location : 'living room',     action   : 'sleeping'   },    sleep: function () {     var custom_url = this.urlroot + this.id + "/action";     this.save({}, { url: custom_url});     // post /cats/{{ cat_id }}/action     // { action: "sleep" }   }, }); 

see here: posting form data using .save() pass url parameters.

you can implement sync method use url if want use custom url on update. see example here: backbone.js use different urls model save , fetch.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -