为什么我的 Backbone.js 片段找不到 DOM 模型?
看一下下面的代码片段:
<script>
$(function(){
var to_username = "alex";
window.Persona = Backbone.Model.extend({
});
window.PersonaList = Backbone.Collection.extend({
model:Persona,
url:function(){
return '/js/personas/approval/user?to_username=' + to_username;
}
});
window.Personas = new PersonaList;
window.CheckView = Backbone.View.extend({
tagName:'div',
template: _.template($("#checkbox_friends").html()),
initialize: function(){
this.model.bind('change', this.render, this);
},
render:function(){
$(this.el).html(this.template({}));
return this;
}
});
window.AppView = Backbone.View.extend({
el:$("#add_friend_holder"),
coreTemplate: _.template($("#add_friend_templ").html()),
initialize:function(){
this.render();
Personas.bind('reset', this.addAllBoxes, this);
Personas.bind('all',this.render, this);
Personas.fetch({
success:function(){
},
error:function(){
}
});
},
addOneBox:function(persona){
var check_view = new CheckView({'model':persona});
this.$("#friend_ticker").append(check_view.render().el); //DOESNT APPEND!!!
},
addAllBoxes:function(){
Personas.each(this.addOneBox);
},
render:function(){
this.el.html(this.coreTemplate({}));
}
});
window.App = new AppView;
});
</script>
<div id="add_friend_holder"></div>
<style>
#friend_ticker{
padding:6px;
border:1px solid #ccc;
background:#fff;
width:200px;
}
</style>
<script type="text/template" id="add_friend_templ">
<button class="btn danger">Add Friend</button>
<div id="friend_ticker">
</div>
</script>
<script type="text/template" id="checkbox_friends">
<input type="checkbox">
Print this
</script>
除了一行之外,一切正常:
this.$("#friend_ticker").append(check_view.render().el);
如果我 console.log(check_view.render().el)
....一切都很好。
除此之外,我的脚本找不到 this.$("#friend_ticker"),即使它就在那里。我没有看到屏幕上打印任何内容。
Take a look at this snippet below:
<script>
$(function(){
var to_username = "alex";
window.Persona = Backbone.Model.extend({
});
window.PersonaList = Backbone.Collection.extend({
model:Persona,
url:function(){
return '/js/personas/approval/user?to_username=' + to_username;
}
});
window.Personas = new PersonaList;
window.CheckView = Backbone.View.extend({
tagName:'div',
template: _.template($("#checkbox_friends").html()),
initialize: function(){
this.model.bind('change', this.render, this);
},
render:function(){
$(this.el).html(this.template({}));
return this;
}
});
window.AppView = Backbone.View.extend({
el:$("#add_friend_holder"),
coreTemplate: _.template($("#add_friend_templ").html()),
initialize:function(){
this.render();
Personas.bind('reset', this.addAllBoxes, this);
Personas.bind('all',this.render, this);
Personas.fetch({
success:function(){
},
error:function(){
}
});
},
addOneBox:function(persona){
var check_view = new CheckView({'model':persona});
this.$("#friend_ticker").append(check_view.render().el); //DOESNT APPEND!!!
},
addAllBoxes:function(){
Personas.each(this.addOneBox);
},
render:function(){
this.el.html(this.coreTemplate({}));
}
});
window.App = new AppView;
});
</script>
<div id="add_friend_holder"></div>
<style>
#friend_ticker{
padding:6px;
border:1px solid #ccc;
background:#fff;
width:200px;
}
</style>
<script type="text/template" id="add_friend_templ">
<button class="btn danger">Add Friend</button>
<div id="friend_ticker">
</div>
</script>
<script type="text/template" id="checkbox_friends">
<input type="checkbox">
Print this
</script>
Everything works except for one line:
this.$("#friend_ticker").append(check_view.render().el);
If I console.log(check_view.render().el)
....it's all good.
Except, my script doesn't find this.$("#friend_ticker"), even though it's right there. I dont' see anything printed on the screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您绑定到两个 Collection 事件时,您是否会在 fetch 后渲染视图 2 次。
第一次调用用子模板填充视图。然后第二个将父级再次呈现为空。
Could it be that you are rendering the view 2 times after fetch as you bind to two Collection events.
The first call fills the view with subtemplates. The second then renders the parent empty again.
ProTom 是正确的,因为视图被渲染了两次。也许您可能想绑定到集合“add”事件而不是“all”。
这是 jsfiddle 作为视图呈现的示例,我手动将一个项目添加到列表中并绑定添加事件的集合。另外,我注释掉了
如果您取消注释该行,您将看到渲染事件被触发两次并“擦除”div 的内容
ProTom is correct in that the view is being rendered twice. Perhaps you may want to bind to the collections "add" event instead of "all".
Here's a jsfiddle as an example where the view renders, I manually added an item to the list and bound the add event of the collection. In addition i commented out
If you uncomment that line you will see that your render event gets fired twice and "erases" the div's content