0

I have fatched the data to Collection variable, but I am not sure what should I do next. How to use this data and to fill the template with it? Here is my code from the render function inside the View.

    Collection.url = "../data";
    Collection.fetch();                         
    var compiled = _.template(self.data);                               
    self.$el.prepend(compiled(/*MY JSON SHOULD GO HERE*/));

I am a newbie to backbone, so every help is appreaciated.

Here is a Collection definition:

var MainCollection = Backbone.Collection.extend({ 
        model: MainModel,
        //localStorage: new Backbone.LocalStorage("kitchen"),
        initialize: function (models,options) { }
    }), Collection = new MainCollection;

Here is a log of Collection and Collection coverted to JSON: enter image description here

hjuster
  • 3,985
  • 9
  • 34
  • 51

1 Answers1

2

Assuming Collection is your collection's name (that's pretty confusing I have to say), this is what you're looking for:

self.$el.prepend(compiled(Collection.toJSON()));

Edit:
Don't forget you're fetching the data asynchronously. So when you're evaluating your template, the data hasn't come back yet, and your collection's still empty. Listen to the end of the request ('sync' event I think) or some other events so you know when the collection's populated OR use the success option of the fetch method to specify a callback :)

As for your logs. When you log an object, it will be automatically updated until you check the details. So you logged it while it was empty, but checked it after it was populated (a few ms afterwards).

Loamhoof
  • 8,293
  • 27
  • 30