I have a simple List-style Backbone app that I’m making with a Rails backend.
I have a collection:
var ItemList = Backbone.Collection.extend({
model: Item,
initialize: function(id) {
this.id = id;
},
url: function(){
return '/lists/' + this.id + '/items';
},
});
All the standard CRUD operations work fine from the model. But I have an “extra” route – “clear” that will clear all the items in a list at one show. The route would be:
/lists/[:id]/clear
Because this is outside the normal CRUD operations, is there way to hook it into the normal Collection, or do i do something separate?
You can make a method on your collection called
destroyand inside there you can take one of several approaches to making the AJAX request (in order of harmony with Backbone). Note you probably don’t want to call your collection methodclearbecause Backbone Models already have aclearmethod with different semantics.Backbone.Modelinstance with the correct URL and ID and then call ‘destroy’ on it$.ajaxcall.