Question

I have a model fetch from a JSON file.

var TemplateModel = Backbone.Model.extend ({

    // JSON URL
    urlRoot: 'json file url',

    // Fetch on initialize.
    initialize:function startModel(){
        this.fetch();
    },
    // Defaults
    defaults: {
        /** Defaults. **/
    }
});

The thing i want to do is to get some Array of objects from the json fetched and load this array to a collection.

var templatesModel = new TemplateModel();
var constants =  templatesModel.get('constants');
var constantsCollection = new Backbone.Collection.extend({model: constants});

But i receive multiple errors.

¿Any idea how can i filter a model and fill with the filtered data a Collection?

Thanks in advance

Was it helpful?

Solution

What kind of errors? Can you show console log?

And:

var constantsCollection = new Backbone.Collection.extend({model: constants});

In this example you define type for models in this collection, and i think this causes errors. If you want to fill collection with models from array try this:

 var constantsCollection = new Backbone.Collection.extend(constants);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top