javascript - Rx.Observable.bindCallback with scope in rxjs - Stack Overflow

It seems in rxjs 4.x, Rx.Observable.fromCallback accept scope as the second parameter, but in 5.0, this

It seems in rxjs 4.x, Rx.Observable.fromCallback accept scope as the second parameter, but in 5.0, this method is changed to Rx.Observable.bindCallback and doesn't accept scope parameter. How to add scope parameter in bindCallback. For example in ES6.

class Test {
  constructor(input) {
    this.input = input;
  }

  callback(cb) {
    return cb(this.input);
  }

  rx() {
    // this works on rx 4.x
    // var observable = Rx.Observable.fromCallback(this.callback, this)();

    // this doesn't work, because this.callback function doesn't use original this, so cannot get this.input
    var observable = Rx.Observable.bindCallback(this.callback)();

    // Work around: Rx.Observable.bindCallback(this.callback)();
    // var me = this;
    // var observable = Rx.Observable.bindCallback((cb) => {me.callback(cb);})();

    observable.subscribe(
      input => console.log('get data => ' + input),
      err => console.log('get error =>' + err),
      () => console.log('plete')
    );
   }
  }

  new Test(100).rx();

It seems in rxjs 4.x, Rx.Observable.fromCallback accept scope as the second parameter, but in 5.0, this method is changed to Rx.Observable.bindCallback and doesn't accept scope parameter. How to add scope parameter in bindCallback. For example in ES6.

class Test {
  constructor(input) {
    this.input = input;
  }

  callback(cb) {
    return cb(this.input);
  }

  rx() {
    // this works on rx 4.x
    // var observable = Rx.Observable.fromCallback(this.callback, this)();

    // this doesn't work, because this.callback function doesn't use original this, so cannot get this.input
    var observable = Rx.Observable.bindCallback(this.callback)();

    // Work around: Rx.Observable.bindCallback(this.callback)();
    // var me = this;
    // var observable = Rx.Observable.bindCallback((cb) => {me.callback(cb);})();

    observable.subscribe(
      input => console.log('get data => ' + input),
      err => console.log('get error =>' + err),
      () => console.log('plete')
    );
   }
  }

  new Test(100).rx();
Share Improve this question edited Jun 14, 2017 at 20:05 Bielik 9922 gold badges15 silver badges26 bronze badges asked Mar 28, 2016 at 7:36 ramon.liuramon.liu 1863 silver badges5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

There is an example at http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-bindCallback which shows how to do this.

Use bindCallback on object method

const boundMethod = Rx.Observable.bindCallback(someObject.methodWithCallback); boundMethod.call(someObject) // make sure methodWithCallback has access to someObject .subscribe(subscriber);

You can call it immediately without declaring a variable, and also pass args like this:

Rx.Observable.bindCallback(someObject.callback).call(someObject,<args>)

So to bind to this you can simply call

Rx.Observable.bindCallback(this.callback).call(this,<args>)

It works for me, when I add this to the constructor

  constructor(input) {
    this.input = input;
    this.callback = this.callback.bind(this)
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745619643a4636441.html

相关推荐

  • javascript - Rx.Observable.bindCallback with scope in rxjs - Stack Overflow

    It seems in rxjs 4.x, Rx.Observable.fromCallback accept scope as the second parameter, but in 5.0, this

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信