javascript - How to setup Rx.Observable.fromEvent to work with jQuery filtered events - Stack Overflow

There are lots of examples of Rx.Observable.fromEvent(element, eventName) using a jquery selection as t

There are lots of examples of Rx.Observable.fromEvent(element, eventName) using a jquery selection as the element to capture events from. However is it possible for Rx listen to only events from a filtered event setup with jQuery?

//Bind the event on body but only respond to events that match the filter
$('body').on('click', '.aClass div .something', function () {...});

//Bind to 'body' but only respond to events from the binding above
Rx.Observable.fromEvent(/*something here?*/);

I have e up with something effectively similar but it seems like it would be much more costly than the jquery filter.

Rx.Observable.fromEvent($('body'), 'click')
.filter(function (e) {
  return $(e.target).is('.aClass div .something');
})
.subscribe(function () {...});

Is there some way I could turn the jQuery binding into an emitter and use that event stream with Rx? What's the best approach?

There are lots of examples of Rx.Observable.fromEvent(element, eventName) using a jquery selection as the element to capture events from. However is it possible for Rx listen to only events from a filtered event setup with jQuery?

//Bind the event on body but only respond to events that match the filter
$('body').on('click', '.aClass div .something', function () {...});

//Bind to 'body' but only respond to events from the binding above
Rx.Observable.fromEvent(/*something here?*/);

I have e up with something effectively similar but it seems like it would be much more costly than the jquery filter.

Rx.Observable.fromEvent($('body'), 'click')
.filter(function (e) {
  return $(e.target).is('.aClass div .something');
})
.subscribe(function () {...});

Is there some way I could turn the jQuery binding into an emitter and use that event stream with Rx? What's the best approach?

Share Improve this question asked Dec 4, 2014 at 18:51 QueueHammerQueueHammer 10.8k12 gold badges69 silver badges93 bronze badges 1
  • see if this helps - xgrommx.github.io/rx-book/content/getting_started_with_rxjs/… – edbond Commented Dec 4, 2014 at 21:14
Add a ment  | 

3 Answers 3

Reset to default 7

see http://jsfiddle/ktzk1bh3/2/

HTML:

<div class="aClass">
    <div>
        <a class="something">Click me</a>
    </div>
</div>

Javascript:

//Bind to 'body' but only respond to events from the binding above
var source = Rx.Observable.create(function(o) {
    $('body').on('click', '.aClass div .something', function(ev) {
        o.onNext(ev);
    })
});

var sub = source.subscribe(function(ev) { console.log("click", ev) });

You can use Rx.Observable.fromEventPattern.

Rx.Observable.fromEventPattern(
  function add(handler) {
    $('body').on('click', '.aClass div .something', handler);
  },
  function remove(handler) {
    $('body').off('click', '.aClass div .something', handler);
  }
);

This way it will automatically remove event handler on unsubscribe from observable subscription.

<div class='radios'>
  <input type='radio' name='r' value='PM'>PM
  <input type='radio' name='r' value='PCE'>PCE
  <input type='radio' name='r' value='PCS'>PCS
</div>
<textarea class='textarea'>

</textarea>

Rx.Observable.fromEvent(document.querySelector('.radios'),'click')
.subscribe((e)=>console.log(e.target.value));

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信