出于某种原因,我无法this
正确传递给回调以呈现视图。我已经尝试过 _.bind 和 _.bindAll 方法,但无论我使用哪种方式传递上下文,我最终都会得到与初始化this
不同的渲染
任何帮助表示赞赏
Navigation.Collection = Backbone.Collection.extend({
model : Navigation.Model,
comparator : function(item) {
return item.get("orderId");
}
});
Menu = new Navigation.Collection();
Navigation.Views.List = Backbone.View.extend({
el : 'nav',
tagName : "div",
className : "navigation",
collection : Menu,
initialize : function(e) {
console.log(this);
this.template = "navigation/list";
this.settings = Settings;
this.collection.on("add", this.render, this);
},
render: function() {
console.log(this);
var renderedContent = this.template(this.collection.toJSON());
console.log(renderedContent);
return this;
},
绑定示例:
initialize : function(e) {
this.collection.on("add", _.bind(this.render, this));
}
bindAll 示例:
initialize : function(e) {
_.bindAll(this, "render");
this.collection.on("add", this.render);
}
控制台输出
第一次打印:
child {cid: "view2", options: Object, views: Object, __manager__: Object, _removeViews: function…}
第二次打印:
Object {resolve: function, resolveWith: function, reject: function, rejectWith: function, notify: function…}
编辑:添加调用“渲染”的位置。这是在代码中定义 Navigation.View.List(见上文)之后立即出现的。
Navigation.registerModule = function(data) {
_.extend(data, {
id : Math.random()
});
Menu.add(new Navigation.Model(data));
Navigation.LayoutManager.removeView(true);
Navigation.cachedRendering = null;
};
Navigation.View = new Navigation.Views.List();
Navigation.LayoutManager = new Backbone.Layout({
views : {
nav : Navigation.View
}
});
Navigation.LayoutManager.$el.appendTo("nav");
Navigation.LayoutManager.render();
return Navigation;
编辑:如果其他人偶然发现同样的事情,解决方案是使用beforeRender
and afterRender
。发生混乱是因为我正在升级依赖项,而旧版本的主干.layoutmanager没有这两个助手,render()
而是使用了 - 带有manage
访问after
状态的参数。