Backbone: fetch collection from server

33,045

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.

Share:
33,045
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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' operator to search for 'id' in {id=MyId, active=true}
        jQuery.jQuery.extend._Deferred.deferred.resolveWith (jquery.js:869)
        done (jquery.js:6591)
        jQuery.ajaxTransport.send.callback
    

    This is the way I created the error:

    var MyModel = Backbone.Model.extend();
    var MyCollection = Backbone.Collection.extend({
        url: '/api/collection',
        model: MyModel
    });
    var coll = new MyCollection();
    coll.fetch();
    

    The elements in /api/collection are parsed in JSON. I tried to return them in various formats

    ["Id1", "Id2", ... ]
    [{id: "Id1, somethingElse: "..."}, ...]
    {id1: { id1s content}, id2: { ... }, ...}
    

    However the error was always the same. What is wrong with this code?

    [Edit] It doesn't help to set an error via coll.fetch({error: errorFunc}); The Exception stays the same.

    [Edit2] Well it seems everything works fine until collection.fetch() calls collection.refresh() with the response object. I have not overwritten any of these functions.

    [Edit3] The error is in the collection.add() method and the reason is that my elements are a list of strings... My server sent them wrong.

  • Julien
    Julien about 13 years
    Backbone expects a key "id" for each record (you can redefine the unique identifier key in your model by redeclaring idAttribute). Then the fetch from collection expects a [] json array.
  • Vlad Gurovich
    Vlad Gurovich about 13 years
    Well, the "id" attribute is only used to determine whether or not the model is a new model or an existing one(meaning upon saving, do we need to create or update it?). The error that happens here, happens upon fetching the models from the server, so I beleive the problem is either bad response format that doesnt get parsed by JSON into an object properly or its simply not in a format that Backbone expects and thus needs to be polished in the "parse" function