我正在尝试开发我的第一个主干应用程序。一切似乎都很好,但是当我渲染视图并将一些 html 附加到 时$el
,页面中没有渲染任何内容。休息服务调用完成,Backbone.Router.extend
内部声明$(document).ready(function () {});
以确保创建 DOM。调试我的javascript,该元素在innerHTMLel
属性中包含正确的值,但是当整个页面呈现时,该值不会出现在页面中。
我究竟做错了什么?
我的查看代码:
window.ProductsListView = Backbone.View.extend({
id: 'tblProducts',
tagName: 'div',
initialize: function (options) {
this.model.on('reset', this.render, this);
},
render: function () {
// save a reference to the view object
var self = this;
// instantiate and render children
this.model.each(function (item) {
var itemView = new ProductListItemView({ model: item });
var elValue = itemView.render().el;
self.$el.append(elValue); // Here: the $el innerHTML is ok, but in the page it disappear. The id of element is div#tblProducts, so the element seems correct
});
return this;
}
});
window.ProductListItemView = Backbone.View.extend({
tagName: 'div',
template: _.template(
'<%= title %>'
),
initialize: function (options) {
this.model.on('change', this.render, this);
this.model.on('reset', this.render, this);
this.model.on('destroy', this.close, this);
},
render: function () {
$(this.el).html(this.template(this.model.toJSON()));
// $(this.el).html('aaaaaa'); // This neither works: it's not a template problem
return this;
},
close: function () {
$(this.el).unbind();
$(this.el).remove();
}
});
我在这里加载产品(内部Backbone.Router.extend
)。这是正确执行的:
this.productsList = new ProductsCollection();
this.productsListView = new ProductsListView({ model: this.productsList });
this.productsList.fetch();
这是我要渲染的 html 元素:
<div id="tblProducts">
</div>
提前致谢,