Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming you're receiving a Category/Items list in JSON that looks something like this...</p> <pre><code>{ 'id': 1, 'name': 'My 1st Category', 'children': [ { 'id': 2, 'name': 'My 2nd Category', 'children': [] }, { 'id': 1, 'name': 'An Item', 'price': 109.99 } ] } </code></pre> <p>Backbone.js doesn't have anything out of the box that supports multiple models in a collection, but it also doesn't have any restrictions on the types of models you put in a collection.</p> <p>Specifying the model type in the collection definition only does one thing, it lets Backbone know what model type to create if you pass raw JSON to the collection instead of a <code>Backbone.Model</code> object. If you add an <code>Item</code> model to a collection that already contains a few <code>Category</code> Models, Backbone will have no problem popping it into the models list; it doesn't do any type checking.</p> <p>So with that in mind, you can use just about everything the collection offers except pass it raw JSON; you'll need to handle that yourself. So your choices are to either build up your models beforehand, making them into <code>Backbone.Model</code> objects, or create something that'll do the parsing for you.</p> <p>For the second option, the parser, I'd suggest passing a special variable to the collection that contains your raw JSON, then handling that in your <code>initialize</code> function. Here's an example:</p> <pre><code>var CategoryCollection = Backbone.Collection.extend({ initialize: function(m, models) { _.each(models, function(model) { var modelObject = null; if (model.price !== undefined) { modelObject = new Item(model); } else { modelObject = new Category(model); } this.add(modelObject); }, this); } }); </code></pre> <p>So it is a little hacky, but you determine the type of model based on if it has a specific field (<code>price</code> in my example), create the model object, then add that to the collection.</p> <p>You'd then call it this way:</p> <pre><code>var myCollection = new CategoryCollection([], myJSON); </code></pre> <p>Notice you have to pass an empty array as the first argument since that's how'd you normally pass a set of models to the collection.</p> <p>Later on when using the collection, you can determine if you're dealing with an <code>Item</code> or <code>Category</code> using a simple <code>instanceof</code> check:</p> <pre><code>_.each(myCollection.models, function(model) { if (model instanceof Item) { console.log("It's an Item! Price: ", model.get("price")); } else { console.log("It's a Category!"); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload