I am having a problem using this.isNew inside my collection and and it looks something like below:
window.MyModel = Backbone.Model.extend({
idAttribute: "_id",
});
window.MyModelList = Backbone.Collection.extend({
alert('Collection');
model: MyModel,
url: function() {
if (this.isNew()) {
alert('show all');
// will ask the server to show all items
return 'api/site/showall'
} else {
alert('create new item');
// will ask the server to create new item
return 'api/site/new'
}
});
It alerts the “Collection” but not the “show all” and “create new item” then I’m getting the error “TypeError:this.isNew is not a function”
Thanks,
It’s true – Collections don’t have an isNew() function, only Models do-
http://backbonejs.org/#Model-isNew
If your goal is to determine if a model is already inside the collection, look into using Underscore’s Find function
http://documentcloud.github.com/underscore/#find