Highway: fetching data from a json file located in the client part

Using Backbone.js, I would like to get data from the Json file in the client when I call the fetch method in an instance of a model or collection.

url propertyin my collection ( MyCol) points to the following json file, which looks like this:

[
  {title: "_My trip to Bali", src: "bali-trip.jpg"},
  {title: "_The flight home", src: "long-flight-oofta.jpg"},
  {title: "_Uploading pix", src: "too-many-pics.jpg"}
]

I execute the following commands:

myCol = new MyCol();
myCol.fetch(); // the Request Method GET is ok 
myCol.toJSON(); // [] 
+3
source share
2 answers

@fguillen correctly says that you can debug your code just by using success and error callbacks. In any case, the error in your json format is due to the members, which should be a string.

Try this and it will work:

[
  {"title": "_My trip to Bali", "src": "bali-trip.jpg"},
  {"title": "_The flight home", "src": "long-flight-oofta.jpg"},
  {"title": "_Uploading pix", "src": "too-many-pics.jpg"}
]
+2
source

, . fetch , .

http://documentcloud.github.com/backbone/#Model-fetch.

- .

myCol = new MyCol();
myCol.fetch({
    success: function() { //This is fired if the request is succesful
        myCol.toJSON();
    }
});

myCol = new MyCol();
myCol.on('change', function() { //This is fired if the model attributes change
   myCol.toJSON();
});
myCol.fetch();

, @roberkules, fetch jqXHR, .

myCol = new MyCol();
myCol.fetch()
     .done(function() {
         myCol.toJSON();
      })
      .fail(function() {
          alert('Oh No!');
      });
+4

All Articles