BackboneJS在一个<ul> -Element </ul>中显示多个集合

时间:2014-05-15 11:39:34

标签: backbone.js handlebars.js backbone-layout-manager

我想知道是否可以在一个<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>

这可能吗?如果是,那么如何?

提前致谢...

[编辑]

好的,所以在我的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>

我做错了什么?

顺便说一句,我使用的是骨干layoutmanager

1 个答案:

答案 0 :(得分:1)

这当然是可能的。渲染集合视图时,只需附加到容器元素(el$el),而不是替换其html。