Skip to content Skip to sidebar Skip to footer

Backbone: Fetch Collection From Server

I'm trying to fetch a collection from my server. I'm using version 0.3.3 (not the master from github) However I am running in this exception: Uncaught TypeError: Cannot use 'in' op

Solution 1:

Since you already identified that your response format is not what Backbone expect, you should override YourModel.parse function that should take the response from your server and return an array of models acceptable by the collection. Here is the snippet from Backbone.js

// **parse** converts a response into a list of models to be added to the// collection. The default implementation is just to pass it through.
parse : function(resp) {
  return resp;
},

As you can see the default function just passes data through. You would have to make it work for your response format.

P.S. id recommend placing a breakpoint in Backbone.fetch method to see what format comes from the server and where exactly it breaks the model creation.

Post a Comment for "Backbone: Fetch Collection From Server"