Asp.net 主干删除模型
在这种情况下,当我单击clear时,即使元素已从DOM中删除,它也不会在firebug中触发Http方法DeleteAsp.net 主干删除模型,asp.net,backbone.js,Asp.net,Backbone.js,在这种情况下,当我单击clear时,即使元素已从DOM中删除,它也不会在firebug中触发Http方法Delete var DecisionItemView = Backbone.View.extend({ tagName: "li", template: _.template($('#item-template').html()), initialize: function () { this.model.bind('c
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();
}
});
你的模特有身份证吗?否则,destroy方法将不会触发http请求 一些代码来检查这个
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);
var M=Backbone.Model.extend({
url:“/echo/json/”
});
var DecisionItemView=Backbone.View.extend({
标记名:“li”,
初始化:函数(){
this.model.bind('change',this.render,this);
this.model.bind('destroy',this.remove,this);
},
活动:{
“单击span.decision destroy”:“清除”
},
渲染:函数(){
var txt=(this.model.get(“id”)?“清除id”:“清除无id”;
$(this.el).html(“+txt+”);
归还这个;
},
清除:函数(){
var answer=confirm(“您确定要删除此决定吗?”);
若有(答复){
这个。模型。破坏({
成功:函数(){
log(“删除成功”);
}
});
}
},
删除:函数(){
$(this.el).remove();
}
});
var m1=新的M({id:1});var m2=新的M();
var view1=新的DecisionItemView({model:m1});
$(“ul”).append(view1.render().$el);
var view2=新的DecisionItemView({model:m2});
$(“ul”).append(view2.render().$el);
感谢您指出,如果模型没有id属性,就不会调用httpdelete方法。实际上,我需要在不进行HTTP Delete调用的情况下销毁一个模型。为了实现这一点,我只需执行
this.unset(“id”)代码>在销毁模型之前。我在主干文档中没有发现这种行为,再次感谢。