0

In this case it wont fire the Http method Delete in firebug when i click clear even if the element is removed from the DOM.

var DecisionItemView = Backbone.View.extend({ tagName: "li", template: _.template($('#item-template').html()), initialize: function () { this.model.bind('change', this.render, this); this.model.bind('destroy', this.remove, this); }, events:{ "click span.decision-destroy": "clear" }, render: function () { $(this.el).html(this.template(this.model.toJSON())); return this; }, clear: function () { var answer = confirm("Are you sure you want to delete this decision?"); if (answer) { this.model.destroy({ success: function () { console.log("delete was a success"); } }); } }, remove: function(){ $(this.el).remove(); } }); 

1 Answer 1

2

Does your model have an id? If not, the destroy method won't fire an http request.

Some code to check this

var M=Backbone.Model.extend({ url:'/echo/json/' }); var DecisionItemView = Backbone.View.extend({ tagName: "li", initialize: function () { this.model.bind('change', this.render, this); this.model.bind('destroy', this.remove, this); }, events:{ "click span.decision-destroy": "clear" }, render: function () { var txt=(this.model.get("id")) ? "Clear with id":"Clear without id"; $(this.el).html("<span class='decision-destroy'>"+txt+"</span>"); return this; }, clear: function () { var answer = confirm("Are you sure you want to delete this decision?"); if (answer) { this.model.destroy({ success: function () { console.log("delete was a success"); } }); } }, remove: function(){ $(this.el).remove(); } }); var m1=new M({id:1}); var m2=new M(); var view1=new DecisionItemView({model:m1}); $("ul").append(view1.render().$el); var view2=new DecisionItemView({model:m2}); $("ul").append(view2.render().$el); 

And a Fiddle http://jsfiddle.net/rSJP6/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out that if a model doesn't have an id attribute, the HTTP Delete method won't be called. I actually needed to destroy a model without making the HTTP Delete call. To accomplish that, I simply perform this.unset("id"); before destroying the model. I found no reference to this behavior in the Backbone docs, so thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.