我正在尝试开发我的第一个骨干应用程序。所有似乎都没问题,但是当我渲染视图并在$el
上附加了一些html时,页面中没有任何内容呈现。 完成其余服务调用,Backbone.Router.extend
在$(document).ready(function() {});
内声明以确保创建DOM。 调试我的javascript,el
元素得到包含在属性中的正确值,但是当整个页面被渲染时,这个值不会出现在页面中。
¿我在做什么错?
我的视图代码:
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元素我想渲染:认为如果中存在的el
提前与酒店
<div id="tblProducts">
</div>
感谢,
你可以用简化的问题创建一个jsfiddle吗?这将大大帮助你.. – 2013-03-06 08:19:24
我没有得到上传的重置服务,所以我的代码只能在我的开发机器上工作。下次我会更好地做好准备。谢谢。 – 2013-03-06 09:24:56