我使用backbone.js,在视图中我有以下观点:
window.PersonListView = Backbone.View.extend({
initialize: function () {
this.render();
},
render: function () {
var people = this.model.models;
$(this.el).html(this.template());
for (var i = 0; i < people.length; i++) {
this.$('#peopleTable tbody').append(new PersonListItemView({model: people[i], no: i+1}).render().el);
}
this.$('#peopleTable').dataTable({"bAutoWidth": false,"iDisplayLength": 50});
return this;
}
});
window.PersonListItemView = Backbone.View.extend({
tagName: "tr",
initialize: function (options) {
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
this.no = options.no;
this.render();
},
render: function () {
$(this.el).html(this.template({
//people stuff
}));
return this;
},
events: {
"click .delete" : "deletePerson",
},
deletePerson: function(event){
event.preventDefault();
if (confirm("Are you sure to delete the person?")) {
this.model.destroy({
success: function () {
location.reload();
}
});
}
return false;
}
});
此人在人员列表中,当他们点击删除时,我想更新列表而不重新加载整个网站。如何修改视图才能执行此操作? 谢谢!
答案 0 :(得分:1)
您只需从列表中删除一项,
即可deletePerson: function(event) {
event.preventDefault();
if (confirm("Are you sure to delete the person?")) {
this.model.destroy({
success: function() {
$(event.currentTarget).closest('tr').remove();
}
});
}
return false;
}