Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbonejs collection.fetch() not working: Cannot call method 'apply' of undefined

Tags:

backbone.js

When executing collection.fetch() the following error is being produced

Uncaught TypeError: Cannot call method 'apply' of undefined
    Backbone.Events.triggerbackbone-0.5.3.js:117
    _.extend._onModelEventbackbone-0.5.3.js:635
    Backbone.Events.triggerbackbone-0.5.3.js:117
    _.extend._addbackbone-0.5.3.js:595
    _.extend.addbackbone-0.5.3.js:448
    _.extend.fetch.options.successbackbone-0.5.3.js:521
    jQuery.Callbacks.firejquery.js:1064
    jQuery.Callbacks.self.fireWithjquery.js:1182
    donejquery.js:7454
    jQuery.ajaxTransport.send.callbackjquery.js:8235

The response from the server

[
  {"title":"The Ampersand Collection","id":"4f1deca441c41f9700000014"},
  {"title":"The Ampersand Collection2","id":"4f1deca441c41f9700000014"},
  {"title":"The Ampersand Collection3","id":"4f1deca441c41f9700000014"},
  //...
]

Code

  $feed.animate({scrollLeft: $(event.target).hasClass('left') ? '-=' + win_width : '+=' + win_width}, {
    step: function(){
      if(!loaded && scope.model.feedlets.last().view !== undefined && !$.rightoffold(scope.model.feedlets.last().view.el, {container: $(scope.model.feedlets.last().view.el).parent(), threshold: 400})) {
        loaded = true;
        scope.model.feedlets.fetch({
          add:true,
          data: {
            limit: 20,
            offset: scope.model.feedlets.models.length
          },
          success: function() {
            console.log(scope.model.feedlets.models);
          }
        });
      }
    },
    complete: function(){
      $('#container').triggerHandler('scroll');
    }
  });

scope is an instance of FeedView which model is an instance of FeedModel. I am using nested collections so each FeedModel has a collection of Feedlets.

like image 742
Ryan Schumacher Avatar asked Dec 31 '25 16:12

Ryan Schumacher


1 Answers

Following the error trace, somewhere in your view you seem to have called bind on the collection's "add" event with an undefined function. So look for something like this within the view:

mycollection.bind('add', this.func, this);

In your code, this.func (whatever it really is) seems to be undefined, or at least it's not a function. Maybe you tried to bind a function before it was created? I'm not sure without seeing the full view code.

like image 177
Kevin Peel Avatar answered Jan 05 '26 07:01

Kevin Peel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!