0

I have a success callback function which returns the following always :

 {"ss":0,"me":"Invalid Username or Password"}

success :function(result){
   console.log("RESULT : "+result.ss);
}

But this always ends up as undefined.. If i print the result, i get the above array. If i return result.ss or result.me i get undefined.

I think there is a very silly reason for why this is happening, but i cannot get my head around.

Backbone View(removing other codes) :

this.model.save({un : username,pd : password, ky : ky}, {
    success :function(result){
        console.log("RESULT : "+result.ss);
        return false;

        if(result.ss==1){
            $("#login_message").addClass('alert-success');


            var userType = result.pp.ut;
    if(userType=="T"){
        window.location.href="trainer/index.html";
    }else if(userType=="C"){
        window.location.href="clients/index.html";
    }else if(userType=="A"){
        window.location.href="admin/index.html";
    }else{
        return false;
    }
     return false;
}   

if(result.ss==0){                       
    console.log(result);
    $("#login_message").addClass('alert-error');
    console.info("Failed to Log In.");
}
return false;

},
    error: function(res){
            console.log(res);
    return false;
    }
    }); 
return false;   
Roy M J
  • 6,926
  • 7
  • 51
  • 78
  • 1
    can you print typeof(result) in the beginning of success function. – ekeren Jul 08 '13 at 10:57
  • 1
    The first argument of the `success` callback is the model itself: `if (success) success(model, resp, options);`. – Loamhoof Jul 08 '13 at 11:04
  • so if this is what i have : var loginview = new LoginView({ model: new UserLogin() }); then the success callback should be : success(loginview, result) ? – Roy M J Jul 08 '13 at 11:06
  • thanks ekeren & Loamhoof.. I was going nuts.. works like charm now.. – Roy M J Jul 08 '13 at 11:08

1 Answers1

1

Backbone success function has the result parameter after model parmeter, you are using model instead of result.

success:function(model, response){}
ekeren
  • 3,408
  • 3
  • 35
  • 55
  • so if this is what i have : var loginview = new LoginView({ model: new UserLogin() }); then the success callback should be : success(loginview, result) ? – Roy M J Jul 08 '13 at 11:06
  • 1
    @RoyMJ the save is on the model not the view. in your example it doesn't seems like you need the model in the callback, so it doesn't really matter how the variable is called. Just use: success:function(model, response){} – ekeren Jul 08 '13 at 11:26