我开始学习 Backbone.js,我从这个样板开始,并通过从磁盘上的静态文件加载 JSON 数据并将其显示在 html 表中来制作示例。
然后我尝试在一个按钮上绑定一个事件,该按钮应该从集合中删除一个元素,然后从 DOM 中删除。事情正常,点击触发destroy
方法,remove
事件在集合上触发,但没有任何结果success
或error
回调destroy
有人有线索吗?
该模型 :
define([
'underscore',
'backbone'
], function(_, Backbone) {
var memberModel = Backbone.Model.extend({
url: "/members.json",
defaults: {
email: "",
firstname: "",
lastname: "",
gender: "",
language: "",
consent: false,
guid: "",
creationDate: ""
},
initialize: function(){
}
});
return memberModel;
});
风景 :
define([
'jquery',
'underscore',
'backbone',
'mustache',
'collections/members',
'text!templates/members/page.html'
], function($, _, Backbone, Mustache, membersCollection, membersPageTemplate){
var membersPage = Backbone.View.extend({
el: '.page',
initialize: function(){
this.members = new membersCollection();
this.members.on('remove', function(){
// works fine
$('.members-body tr').first().remove();
console.log('removed from collection');
});
},
render: function () {
var that = this;
this.members.fetch({success: function(){
var wrappedMembers = {"members" : that.members.toJSON()};
that.$el.html(Mustache.render(membersPageTemplate, wrappedMembers));
$('#delete-member').click(function(){
that.members.at(0).destroy({
// prints nothing!!!
success: function(){ console.log('sucess'); },
error: function(){ console.log('error'); }
});
});
}});
}
});
return membersPage;
});