Jay
Jay

Reputation: 938

BackboneJS, having trouble with success and error when saving a model

For some reason, I cannot enter my success and error blocks when I am saving my model. Wether my response is successful "201" or error "404", my code will not hit the debugger lines. Does anyone know what could be going wrong?

SignInView.prototype.login = function(event) {
  event.preventDefault();
  return this.model.save(this.credentials(), {
    type: 'POST',
    url: 'http://localhost:3001/api/v1/users/sign_in'
  }, {
    success: (function(_this) {
      return function(userSession, response) {
        debugger;
        return window.location.href = "/";
      };
    })(this),
    error: (function(_this) {
      return function(userSession, response) {
        debugger;
        var message;
        message = $.parseJSON(response.responseText).error;
        return alert(message);
      };
    })(this)
  });
};

Upvotes: 0

Views: 43

Answers (1)

lucasjackson
lucasjackson

Reputation: 1525

The save function only takes two parameters -- you are passing your success and error functions as a third param. Try the following:

SignInView.prototype.login = function(event) {
  event.preventDefault();
  return this.model.save(this.credentials(), {
    type: 'POST',
    url: 'http://localhost:3001/api/v1/users/sign_in',
    success: (function(_this) {
      return function(userSession, response) {
        debugger;
        return window.location.href = "/";
      };
    })(this),
    error: (function(_this) {
      return function(userSession, response) {
        debugger;
        var message;
        message = $.parseJSON(response.responseText).error;
        return alert(message);
      };
    })(this)
  });
};

Upvotes: 1

Related Questions