[RxJS] Creation operators: fromEventPattern, fromEvent

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');
});
上一篇:SQL中GROUP BY用法示例(转)


下一篇:大华摄像头报警接口中图片加密,python调用c++方式实现解密