Reputation: 6664
I have a backbone collection of models.
MyCollection = Backbone.Collection.extend({
model: myMymodel;
});
MyModel = Backbone.Model.extend({
...
});
Each model has a view
myView = Backbone.View.extend({
initialize: function() {
this.model = new MyModel();
};
});
There is no persistence on the server-side. This is just for structuring client-side information. So the models do not have ids, and a url for Backbone.sync has not been configured.
From within the view, I want to remove the model from the collection.
I have tried the following:
this.model.trigger( "destroy" );
However it does not work. The destroy event is not propagating to the collection.
Any idea what I'm doing wrong?
Thanks,
Upvotes: 0
Views: 3405
Reputation: 10743
I think you are not instantiating the collection at all. Cant make out that from the code at least. If you are just creating a model instance but not adding it to any collection, this.model.trigger("destroy");
will do nothing.
myView = Backbone.View.extend({
initialize: function() {
this.coll = new MyCollection();
this.model = new MyModel();
this.coll.add(this.model);
};
});
Now that the model is part of the collection:
this.model.destroy()
Makes a delete api call and gets removed from the collection
this.collection.remove(this.model)
Removes the model from the collection but does not make a delete api call.
this.model.trigger("destroy");
Triggers a destroy event on the model but does not destroy the model as such. sames as collection.remove(this.model)
if model is part of the collection.
Upvotes: 2
Reputation: 10104
collection.remove(model)
would be a more appropriate function to use since you're not persisting your models on the server-side. Backbone.Collection.remove
Upvotes: 0