[英]Backbone.js refresh view without reloading page
我正在使用骨干.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;
}
});
该人员在人员列表中,当他们单击“删除”时,我想在不重新加载整个网站的情况下更新列表。 我必须如何修改视图才能做到这一点? 谢谢!
您只需从列表中删除一项,
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.