feronovak
feronovak

Reputation: 2697

Backbone throwing collection[method] function error

i have been playing with Backbone, I am trying to learn how it works so I can create phonegap app using backbone. With version 0.9.9 all worked well, after upgrade to 0.9.10 it returns:

TypeError: collection[method] is not a function

   collection[method](resp, options);

backbone-0.9.10.js (line 821)

It seems that problem has something to do with following sections:

var params = _.extend({
    'method': 'GET',
    'url': this.url,
    'cache': true,
    'dataType': 'json',
    'processData': true
}, options);

console.log(params);

return $.ajax(params);

I am unable to find out what is wrong.

Working version with 0.9.9

http://92.245.6.92/backbone.peoples/index.html

Upgraded and not working version with 0.9.10

http://92.245.6.92/backbone.peoples/index2.html

Could you please help me whether there is major fault in my code or what happened. 0.9.10 is release candidate for 1.0 so I expect this not to work with any new version.

Thanks.

Upvotes: 2

Views: 329

Answers (1)

jevakallio
jevakallio

Reputation: 35970

The fetch success callback signatures was changed from 0.9.9 to 0.9.10. The callback signature is now

function(collection, resp, options) { ...

In your app you've overridden Collection.sync, and you execute the callback with (app.js:35):

options.success = function(data, textStatus, jqXHR) {
    model.parse(data);
    if(success)
        success(data, textStatus, jqXHR);
};

This leads to a situation where Backbone tries to call a method reset on the data object, which is a vanilla javascript array and doesn't have such a method. To comply to the new API, you need to call it with:

var collection = this;
options.success = function(data, textStatus, jqXHR) {
    model.parse(data);
    if(success)
        success(collection , data, options);
};

Upvotes: 3

Related Questions