Search code examples
backbone.jscollectionsview

backbone js using multiple collection in view


I am quite new in backbone js . I was reading the documentation of backbone and I come up with this idea to use multiple collection in view. If I have single view and I want to use more than one collection how can I achieve it? how can the view understand multiple collection? thanks.


Solution

  • Why do you ever need to use multiple collection inside a single view? Backbone's strength is it's modularity, which means you can develop the whole structure of your application by building it's components piece-by-piece. So in normal circumstances one view has one model or collection of models, but it's acceptable and it's often used the case when a collection has multiple views (for example a chat system).

    To have multiple collections inside a single view is against backbone's modularity principle. Breaking up views to respond to only one model/collection turns out much more modular and reusable code.

    So i suggest to break up your application into smaller pieces and operate on segment level, but if you really need to preserve the current structure you can do something like this:

    var view = new MyView({
      collection: {
        users: new UsersCollection(),
        organization: new OrganizationCollection()
      }
    });