BackboneJS Display multiple collections in one <ul>-Element

116 views Asked by At

I would like to know if it's possible to diplay multiple collections inside one <ul>-element?

for example when I have multiple views, in each view I have a collection like this:

Collection = Backbone.Collection.extend({
    url: function() {
        return '/path/to/JSON/file';
    }
});

Now I want to display all Collections inside the same <ul>-element so that in my frontend it would give me the output

<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>

Is that possible? If yes then how?

Thanks in advance...

[EDIT]

Ok, so in my mainView I have:

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();

Like mentioned before, each Collection is bound to its own view:

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;
}

The same above goes for Twitter and Facebook. So I have 3 modules.

Then in the template artistchannel there is a section where I have:

<div class="socialMediaDiv">
    <ul class="socialMedia"></ul>
 </div>

And my HandlebarsJS-HTML template looks like:

{{#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}}

When I do it like this it generates 3 <li>-elements, so the structure is like:

<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>

What am I doing wrong??

By the way, I'm using backbone's layoutmanager

1

There are 1 answers

1
Stephen Thomas On

It's certainly possible. When you render the collection view, just append to the container element (el or $el) instead of replacing its html.