rea*_*ers 5 javascript backbone.js
我正在使用backbone.js来实现一个名为Roster的好友列表.我对名册收集和个人的主干观点rosterEntry如下:
Slx.Roster.Views.RosterEntry = Backbone.View.extend({
tagName: "li",
className : "rosterEntry clearfix",
templateSelector: '#rosterEntryTemplate',
initialize: function() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.model.bind('remove', this.remove);
this.template = _.template($("#rosterEntryTemplate").html());
},
remove: function () {
debug.log("Called remove event on model");
$(this.el).remove();
},
render: function() {
var renderedContent = this.template(this.model.toJSON());
this.id = this.model.Id;
$(this.el).attr('id', "friendid-" + this.model.get("id")).html(renderedContent);
return this;
}
});
Slx.Roster.Views.Roster = Backbone.View.extend({
el: "div#roster-container",
initialize: function () {
_.bindAll(this, 'render', 'add', 'remove');
this.template = _.template($("#rosterTemplate").html());
this.collection.bind('reset', this.render);
this.collection.bind('add', this.add);
this.collection.bind('remove', this.remove);
},
add: function (rosterEntry) {
var view = new Slx.Roster.Views.RosterEntry(
{
model: rosterEntry
});
$(this.el).append(view.render().el);
},
remove: function (model) { // if I ommit this then the whole collection is removed!!
debug.log("called remomve on collection");
},
render: function () {
var $rosterEntries, collection = this.collection;
$(this.el).html(this.template({}));
$rosterEntries = this.$('#friend-list');
_.each(collection.models, function (model) {
var rosterEntryView = new Slx.Roster.Views.RosterEntry({
model: model,
collection: collection
});
$(this.el).find("ul#friend-list").append(rosterEntryView.render().el);
}, this);
return this;
}
})
Run Code Online (Sandbox Code Playgroud)
我正在使用Firebug控制台进行测试,并且可以通过执行以下命令来填充名单:
collection = new Slx.Roster.Collection
view = new Slx.Roster.Views.Roster({collection:collection})
collection.fetch()
Run Code Online (Sandbox Code Playgroud)
通过在Firebug控制台中执行以下操作,添加到集合也可以正常工作:
collection.add(new Slx.Roster.Model({username:"mickeymouse"})
并将新的rosterEntry内容添加到名册中.
我的问题是collection.remove(5)从内存中的集合中删除,但没有更新DOM.
奇怪的是,如果我remove()从名单视图中省略该功能,则会删除名单中的所有条目.如果我添加此方法除了控制台日志之外没有任何内容,则会RosterEntry调用名单和视图上的remove方法- 虽然我不确定为什么但是它们不正常!
["Called remove event on model"]
["called remomve on collection"]
Run Code Online (Sandbox Code Playgroud)
如果我从RosterEntry模型中删除remove函数,我会收到此错误:
TypeError: this.$el is undefined
this.$el.remove();
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?从集合中删除元素时如何从DOM中删除元素?
我认为您对每个事件绑定定义都有问题context。
尝试改变这一点:
this.model.bind('remove', this.remove);
Run Code Online (Sandbox Code Playgroud)
为了这:
this.model.bind('remove', this.remove, this);
Run Code Online (Sandbox Code Playgroud)
我知道您已尝试使用 来解决此问题bindAll,但正在使用实际对象的上下文包装对列出的方法的每个调用,但如果您在其他对象上bindAll调用相同的方法,这将无济于事:)。
我读了更多内容......看起来bindAll和命令的第三个参数的作用bind完全相同。所以也许你可以使用其中之一。
因为它不起作用bindAll是model.remove因为您忘记将其添加到_.bindAll(this, 'render')行中,请查看您的代码。我的建议解决了这个问题,因为正如我所说,两种方法的作用相同。
所有这一切的作用是确保对列出的方法的调用(render, remove, ...在一种情况或this.remove另一种情况下)将解释this为对实际对象的引用。这看起来很愚蠢,但在 JS 中this却非常不稳定。
如果您仍然对这个问题感到困惑,请不要担心this,我已经处理它很长时间了,但仍然不完全一致。
也许这样的文章可以帮助我们。
| 归档时间: |
|
| 查看次数: |
16986 次 |
| 最近记录: |