Locohost
Locohost

Reputation: 1702

backbonejs collection.fetch error handler

The following backbone collection.fetch code fires, errors for some reason and then jumps to the error handler (as expected), but I don't really know what the errorhandler params are. The model, xhr and options params are undefined when error fires. What am I doing wrong?

  var onErrorHandler = function(model, xhr, options) {
      alert(options);
  };

  that.collection = new MembersCollection([]); 
  that.collection.fetch({ success : onDataHandler, error: onErrorHandler, dataType: "jsonp" });

@muistooshort: I totally forgot about the js arguments, thanks for that tip.

Here is what I found...

Arguments[0] = looks like its just the letter "d"
Arguments[1] = is an object. Has readyState, responseText, status, statusText
Arguments[2] = is an object. Exactly the same as [1]

The status = 200 and text is "OK". The responseText is the exact JSON data I expected to receive from the PHP server model.

So I guess now the question is why is that collection.fetch method sending the success result to the error handler? I don't believe the sequence of those handlers in the fetch callbacks matter. Does it?

Upvotes: 9

Views: 20831

Answers (1)

Locohost
Locohost

Reputation: 1702

Ok I found the params signature for the fetch callbacks and the success/error handlers. Now that those are set correctly, the fetch is working as expected. This is working code...

  var onDataHandler = function(collection, response, options) {
      console.log('membersview fetch onedatahandler');
      that.render();
  };

  var onErrorHandler = function(collection, response, options) {
      console.log('membersview fetch onerrorhandler');
      alert(response.responseText);
  };

  that.collection = new MembersCollection([]); 
  that.collection.fetch({ success : onDataHandler, error: onErrorHandler });

Thank you guys for your replies. I greatly need/appreciate your advice :-)

Upvotes: 16

Related Questions