Backbone.js - Getting JSON back from url

Posted by Brian on Stack Overflow See other posts from Stack Overflow or by Brian
Published on 2012-06-07T04:29:16Z Indexed on 2012/06/07 4:40 UTC
Read the original article Hit count: 214

Filed under:
|
|

While trying to learn Backbone.js, I've been trying to grab the content of a JSON file using the following code:

(function($){
    var MyModel = Backbone.Model.extend();
    var MyCollection = Backbone.Collection.extend({
        model : MyModel,
        url: '/backbone/data.json',
        parse: function(response) {
          console.log(response);
          return response;
        }
    });

    var stuff = new MyCollection;
    console.log(stuff.fetch());
    console.log(stuff.toJSON());
})(jQuery)

'stuff.fetch()' returns the entire object (with the data I'm after in responseText), 'stuff.toJSON' returns nothing ([]), but the console in the parse method is returning exactly what I want (the json object of my data).

I feel like I'm missing something obvious here, but I just can't seem to figure it out why I can't get the right data out. Could someone point me in the right direction or show me what I'm doing wrong here? Am I using a model for the wrong thing?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON