How can I bind this to a callback function, using call or apply?
beforeModel: function(params){
return this.get('session').authenticate().then(function(){
this.findClient(params);
}.bind(this)).catch(function(err){
//retrieve new session
return this.get('session').authorize().then(function(){
this.findClient(params);
}.call(this));
});
},
Throws an error at some point:
TypeError: this.get is not a function
This should referring to Ember Controller scope, why if I bind first with bind(this) and then call(this) throws an error?
You are better using
bind
for that.MDN
Edit: You are still making the same mistake. The promise method
then
takes a function reference, so it can call it once it's settled. What you are doing is executing the function and passing to thethen
method the returned value of the function which in this case is another promise object.Lets break it down:
!
-commented line was changed by me. You bound thesuccess
function correctly, but not thefailure
function. Easy thing to miss!