您的位置:首页 > 运维架构

[RxJS] Creation operators: fromEventPattern, fromEvent

2016-04-14 00:57 555 查看
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');
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: