简体   繁体   English

将此绑定到回调函数

[英]Bind this to a callback function

How can I bind this to a callback function, using call or apply? 如何使用call或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 TypeError:this.get不是一个函数

This should referring to Ember Controller scope, why if I bind first with bind(this) and then call(this) throws an error? 这应该指的是Ember Controller作用域,为什么如果我先用bind(this)绑定然后调用(this)会引发错误?

You are better using bind for that. 您最好为此使用bind

function doSomething(){
    return authorize().then((function(){

       }).bind(this));
}

The bind() function creates a new function (a bound function) with the same function body (internal call property in ECMAScript 5 terms) as the function it is being called on (the bound function's target function) with the this value bound to the first argument of bind(), which cannot be overridden. bind()函数创建一个新函数(绑定函数),该函数具有与要调用的函数(绑定函数的目标函数)相同的函数主体(ECMAScript 5术语为内部调用属性),并且此值绑定到bind()的第一个参数,不能被覆盖。

MDN 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. 该承诺的方法then将一个函数引用,所以它可以调用它,一旦它的解决。 What you are doing is executing the function and passing to the then method the returned value of the function which in this case is another promise object. 您正在执行的是执行该函数并将该函数的返回值传递给then方法,在这种情况下,该返回值是另一个promise对象。

Lets break it down: 让我们分解一下:

beforeModel: function(params) {
  //Function to be passed down to your promise to find a Client.
  //Note that nothing gets passed down to the next step once the promise gets settled.
  var fClient = function() {
    this.findClient(params);
  };
  //We want this to be this (of beforeModel function).
  fClient = fClient.bind(this);

  //I have no idea what you are trying to do with the catch...
  var reAttachPromise = function() {
    return this.get('session').authorize().then(fClient);
  };
  //We want this to be this (of beforeModel function).
  reAttachPromise = reAttachPromise.bind(this);

  return this.get('session').authenticate().then(fClient).catch(reAttachPromise);
  //can be reduced to:
  // return reAttachPromise().catch(reAttachPromise);
}
    }.bind(this)).catch(function(err){
      //retrieve new session
      return this.get('session').authorize().then(function(){
        this.findClient(params);
      }.call(this));
    }.bind(this)); //!!

! -commented line was changed by me. -评论线被我改变了。 You bound the success function correctly, but not the failure function. 您正确绑定了success功能,但没有绑定failure功能。 Easy thing to miss! 容易错过的事情!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM