0
在這種情況下,它不會觸發Http方法刪除螢火蟲當我點擊清除,即使元素從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();
}
});
感謝您指出,如果模型沒有一個id屬性,該HTTP刪除方法不會被調用。我實際上需要銷燬一個模型而不進行HTTP Delete調用。爲了實現這一點,我只需在銷燬模型之前執行'this.unset(「id」);''。我發現Backbone文檔中沒有提到這種行爲,所以再次感謝。 – 2012-07-26 22:46:19