Javascript 获取成功后主干视图未呈现
我是主干网的新手,正在尝试组装一个小应用程序,但在获取客户端渲染视图时遇到了问题 这是我的客户机在翡翠htmlJavascript 获取成功后主干视图未呈现,javascript,node.js,backbone.js,pug,backbone-views,Javascript,Node.js,Backbone.js,Pug,Backbone Views,我是主干网的新手,正在尝试组装一个小应用程序,但在获取客户端渲染视图时遇到了问题 这是我的客户机在翡翠html extends layout block content .row #breadcrumbs.span12 script#room-list-template(type="text/template") <td><a href="#"><%=name%></a></td> <td>
extends layout
block content
.row
#breadcrumbs.span12
script#room-list-template(type="text/template")
<td><a href="#"><%=name%></a></td>
<td><button class="btn btn-info">Join Room</button></td>
script(src="/javascripts/dislocated_poker/index.js").
script(src="/javascripts/dislocated_poker/nav.js").
script(src="/javascripts/dislocated_poker/room.js").
script(type="text/javascript").
$(function(){
DislocatedPoker.init();
})
})
这里是我的观点和模型
DislocatedPoker.Room = Backbone.Model.extend({
});
DislocatedPoker.Rooms = Backbone.Collection.extend({
model : DislocatedPoker.Room,
url : "/api/rooms"
});
DislocatedPoker.RoomView = Backbone.View.extend({
tagName : "tr",
render : function() {
var template = $("#room-list-template").html();
var compiled = _.template(template, this.model.toJSON());
$(this.el).html(compiled);
return this;
}
})
DislocatedPoker.RoomListView = Backbone.View.extend({
initialize : function() {
this.collection.bind("reset", this.render, this);
this.collection.bind("add", this.render, this);
},
tagName : "table",
className : "table table-striped",
render : function() {
var els = [];
this.collection.each(function(item) {
var itemView = new DislocatedPoker.RoomView({model : item});
els.push(itemView.render().el);
})
//return this;
$(this.el).html(els);
$("#room-list").html(this.el);
}
}
我看到从fetch方法返回JSON,并对集合进行迭代,但结果永远不会以客户端html结束。如果我查看HTML的源代码,我会看到以下模板应该呈现的位置
<script id="room-list-template" type="text/template"><td><a href="#"><%=name%></a></td>
<td><button class="btn btn-info">Join Room</button></td>
我觉得我遗漏了一些非常明显的东西,但似乎无法准确指出问题所在
非常感谢您的指导
谢谢。看来以下方法行不通:
$(this.el).html(els);
jQuery的html函数接受一个字符串,您提供了一个数组。尝试:
$(this.el).html(els.join(""));
您应该尝试以下方法:
this.collection.bind("fetched", this.render, this);
您是否尝试用on替换bind?@Evgeniy,bind==on。换句话说,bind是on的别名。
this.collection.bind("fetched", this.render, this);