我想知道是否有可能在一个<ul>
-元素内显示多个集合?
例如,当我有多个视图时,在每个视图中我有一个这样的集合:
Collection = Backbone.Collection.extend({
url: function() {
return '/path/to/JSON/file';
}
});
现在我想在同一个<ul>
-元素中显示所有的集合这样在我的前端就会输出
<ul>
<li>Data from Collection 1</li>
<li>Data from Collection 1</li>
<li>Data from Collection 2</li>
<li>Data from Collection 2</li>
<li>Data from Collection 3</li>
<li>Data from Collection 3</li>
</ul>
这可能吗?如果是,那怎么做?
Thanks in advance…
[编辑]
好的,在mainView中我有:
Artist.View = Backbone.View.extend({
template: 'artistchannel',
beforeRender: function() {
var artistinstagramCollection = new ArtistInstagram.ArtistInstagramCollection();
artistinstagramCollection.artist_id = this.artist_id;
this.insertView('.socialMedia', new ArtistInstagram.View({collection:artistinstagramCollection}));
artistinstagramCollection.fetch();
var artisttwitterCollection = new ArtistTwitter.ArtistTwitterCollection();
artisttwitterCollection.artist_id = this.artist_id;
this.insertView('.socialMedia', new ArtistTwitter.View({collection: artisttwitterCollection}));
artisttwitterCollection.fetch();
var artistfacebookCollection = new ArtistFacebook.ArtistFacebookCollection();
artistfacebookCollection.artist_id = this.artist_id;
this.insertView('.socialMedia', new ArtistFacebook.View({collection: artistfacebookCollection}));
artistfacebookCollection.fetch();
如前所述,每个Collection都绑定到自己的视图:
function (App, Backbone) {
var ArtistInstagram = App.module();
ArtistInstagram.View = Backbone.View.extend({
tagName: 'li',
template: 'instagram',
initialize: function() {
this.listenTo(this.collection, 'all', this.render)
},
serialize: function() {
return this.collection ? this.collection.toJSON() : [];
}
});
ArtistInstagram.ArtistInstagramCollection = Backbone.Collection.extend({
url: function() {
return '/myproject/index.php/api/social_feeds/instagram/' + this.artist_id;
}
});
return ArtistInstagram;
}
Twitter和Facebook也是如此。所以我有3个模块
然后在模板artistchannel
中有一个部分,我有:
<div class="socialMediaDiv">
<ul class="socialMedia"></ul>
</div>
我的HandlebarsJS-HTML模板看起来像:
{{#each this}}
<li>
<a href="{{link}}" target="_blank"><img src="{{title}}" /></a>
<section class="ip">
<p>{{description}}</p>
<h3>
<time class="timeago" datetime="{{pubDate}}"></time>
{{type}}
</h3>
</section>
</li>
{{/each}}
当我这样做时,它生成3个<li>
-元素,因此结构如下:
<ul>
<li>
<li>some results from collection 1</li>
<li>...</li>
<li>...</li>
<li>...</li>
</li>
<li>
<li>some results from collection 2</li>
<li>...</li>
<li>...</li>
<li>...</li>
</li>
<li>
<li>some results from collection 3</li>
<li>...</li>
<li>...</li>
<li>...</li>
</li>
</ul>
我做错了什么?
顺便说一下,我使用的是backbone的layoutmanager
这当然有可能。当您呈现集合视图时,只需附加到容器元素(el
或$el
),而不是替换它的html。