我正在调用带有自身 html 的列表元素的视图,在此视图中存在对单个项目的另一个视图的迭代调用。问题是 listelement 的第一个视图看不到自己的 html。如果我将 el 设置为获取 id (el:$("#pagination-centered") ) ,我想在其中追加迭代单个元素,不要渲染。
列表元素的视图:
define(["jquery", "underscore", "Backbone", "Handlebars",
"views/singlepostview", "text!templates/home.html"],**<--html**
function ($, _, Backbone, Handlebars,SinglePost, template) {
var Home = Backbone.View.extend({
el:$("#pagination-centered"),//it is inside home.html defined above
template: Handlebars.compile(template),
initialize: function () {
// this.model.bind("reset", this.render, this);
},
events: {
},
render: function (eventName) {
$(this.el).empty();
_.each(this.model.models, function (ad) {
$(this.el).append(new SinglePost({
model: ad
}).render().el);
}, this);
return this;
}
});
return Home;
});
单元素视图:
define(["jquery", "underscore", "Backbone", "Handlebars", "text!templates/singlepost.html"],
function ($, _, Backbone, Handlebars, template) {
var SinglePost = Backbone.View.extend({
//el:$("#pagination-centered"),
//tagName: "li",
events: {
"click": "goToDetails"
},
template: Handlebars.compile(template),
initialize: function () {
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
},
render: function (eventName) {
var ad = this.model.toJSON();
// console.log(ad);
ad.cid = this.model.cid;
$(this.el).html(this.template(ad));
return this;
},
goToDetails: function () {
Backbone.history.navigate("#user/"+this.model.id, {trigger: true});
}
});
return SinglePost;
与视图主页关联的模板(元素列表):
<div class="row">
<div class="page-header ">
<p class="pull-right"><a href="#">see all</a></p>
<h4>Honors <small>(10)</small></h4>
</div>
<div class="pagination-centered">
</div>
</div>
与查看 SinglePost(列表项)关联的模板:
<a href="#"><img src="{{immagine.url}}" class="img-circle"></a>
我想在主页视图的模板中,特别是在 div class="pagination-centered" 中渲染这个(最后一个)singlepost 视图的 singlepost 模板。将作为:
**<div class="row">
<div class="page-header ">
<p class="pull-right"><a href="#">see all</a></p>
<h4>Honors <small>(10)</small></h4>
</div>
<div class="pagination-centered">
*<a href="#"><img src="{{immagine.url}}" class="img-circle"></a>*
</div>
</div>**