Is it possible to change the URL that gets loaded when create() is called on a collection? I am doing it on models as noted here:
Backbone.js Model different url for create and update?
Here is an example of my collection code:
var GroupsCollection = Backbone.Collection.extend({
model:Group,
url:"/api/groups/get_all/",
parse:function(res){
return res.groups;
},
sync: function(method, model, options) {
var methodUrl = {
'add': '/api/group/create/'
};
if (methodUrl && methodUrl[method.toLowerCase()]) {
options = options || {};
options.url = methodUrl[method.toLowerCase()];
}
Backbone.sync(method, model, options);
}
});
I coped the sync function from my model class (Group). I am using the key ‘add’ in methodUrl because the Backbone documentation says an “add” event is triggered when create() is called. I’ve tried “create” instead of “add” as well (doesn’t work).
Calling create() always just loads the url: “/api/groups/get_all”
Help? Open to any ideas about best practices around this stuff too. Thanks!
I’d say it might be easier to read if you could folow the rest “convention” and let Backbone handle your URL with it’s default. (It’s pretty easy to switch on request.method in most servers).
Anyway, you could simply change the URL at creation time :