码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Creation operators: fromEventPattern, fromEvent

时间:2016-04-14 01:22:55      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

Besides converting arrays and promises to Observables, we can also convert other structures to Observables. This lesson teaches how we can convert any addEventHandler/removeEventHandler API to Observables.

 

 fromEvent(target, EventType):
var foo = Rx.Observable.fromEvent(document, ‘click‘);

foo.subscribe(function (x) {
  console.log(‘next ‘ + x);
}, function (err) {
  console.log(‘error ‘ + err);
}, function () {
  console.log(‘done‘);
});

 

fromEventPattern(addEventHandler, removeEventHandler): take two functions

function addEventHandler(handler){
  document.addEventListener(‘click‘, handler)
}

function removeEventHandler(handler){
  document.removeEventListener(‘click‘, handler)
}

var foo = Rx.Observable.fromEventPattern(addEventHandler, removeEventHandler);

foo.subscribe(function (x) {
  console.log(‘next ‘ + x);
}, function (err) {
  console.log(‘error ‘ + err);
}, function () {
  console.log(‘done‘);
});

 

[RxJS] Creation operators: fromEventPattern, fromEvent

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5389543.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!