admin管理员组

文章数量:1024638

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)
  }

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)
  }

本文标签: javascriptRxObservablebindCallback with scope in rxjsStack Overflow