Question

I have a json file that i want to filter and create a collection of this filtered json.

How can i do this?

file.json
->Calculators
->Constants
->Contents

I want to filter the file.json and make a collection from Constants Object Items.

Is it posible to make this?

The problem i'm having is that I can't filter the json, only pass all the variables of all the json to the model.

I have read and searched here on stackoverflow.

Thanks in advance.

Was it helpful?

Solution

Do it in the parse method of your backbone collection:

var myCollection = Backbone.Collection.extend({

  parse:function(data){
    return data.Constants;
  }    

});

This will return only the part of your json your want to be loaded into your collection.

Edit to give you some basics after your comment:

Hopefully your data.Constants contains an array of objects you want to be loaded into your collection as models. Of course you will need a model:

var myModel = Backbone.model.extend({});

Then you link that model to your Collection:

var myCollection = Backbone.Collection.extend({

  model: myModel,

  url: 'path/to/your/file.json',

  parse:function(data){
    return data.Constants;
  }    

});

And you fetch your collection:

var c1 = new myCollection();
c1.fetch();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top