0
votes

I want to load JSON data from file and load into a Collection.

Collection:

define(['backbone', 'model'], function(Backbone, Model) {
    return Backbone.Collection.extend({
        model: Model,
        url: 'data/data.json'
    });
});

Edit: The problem now seem to be the data is collected after the render function is executed the first time. So if I comment out the render function and make the template update from the success function, it works, but this is of course not the proper way of doing it. Any better ideas?

1
do you have any error ? Does your "ContentCollection" ( bad copypasta by the way in the second snippet... ) is null or not ? In newest version of backbone, you need to listen to the sync event, not reset, that's maybe your problem. - Pierre Gayvallet
I don't get any errors, other than my template isn't getting it's data. I've tried something like: this.collection.fetch({add: true, success: function(collection, response){ collection.trigger('sync'); }}); Still not working. - user1121487

1 Answers

0
votes
this.template(this.coll.toJSON()) 

will probably solve your problem. Never use the collection itself when forwarding data to template.

If you're using handlebars or mustache, you should event use :

this.template({col : this.coll.toJSON()})

it's usually good practice to not use an array as root element for context.