[Javascript + rxjs] Using the map method with Observable
Like an array, Observable has a map method that allows us to transform a sequence into a new Observable.
var Observable = Rx.Observable; //Create click events by Observable
var clicks = Observable.fromEvent(button, 'click');
var points = clicks.map(function(e) {
return {x: e.clientX, y: e.clientY};
}); //Then we are able to use forEach, concatAll, map, fliter function
//The function return an subscription object, which we can use to call dispose() method to remove listener
var subscription = points.forEach(function onNext(point) {
console.log('clicked:' + JSON.stringify(point));
//subscription.dispose();
}, function onError() {
console.log('error');
}, function onCompleted() {
console.log('complete');
});
We change forEach method on points:
var subscription = points.forEach(function onNext(point) {
So it print out each click's position.
var points = clicks.map(function(e) {
return {x: e.clientX, y: e.clientY};
});
creates another Observable object, remember once you use observable object to generate another object by foreach, map or concatAll method, this object would be observable.
observable is lazy:
If we comment out this part of code:
/*var subscription = points.forEach(function onNext(point) {
console.log('clicked:' + JSON.stringify(point));
//subscription.dispose();
}, function onError() {
console.log('error');
}, function onCompleted() {
console.log('complete');
});*/
Then you click the button, nothing happend.
As a matter of fact, addEventListener, under the hood, has not even been called by Observable.fromEvent.
The way Observable works is it waits until you call forEach to have any side effects, to carry out any side effects whatsoever. What we've done is we've really just built an Observable that promises that when you will call forEach on it, it will hook up an event listener.
When we map over that Observable, we've created another Observable that promises that when we forEach over it, it will forEach over the underlying data source, clicks, and then, as data arrives, will transform that data, using a projection function, into new data. Just simply creating Observables causes nothing to happen.
We have to forEach over the Observable in order for something to happen.
[Javascript + rxjs] Using the map method with Observable的更多相关文章
- [Javascript] The Array map method
One very common operation in programming is to iterate through an Array's contents, apply a function ...
- Javascript中Array.prototype.map()详解
map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数.callback 每次执行后的返回值组合起来形成一个新数组. callback 函数只会在有值的索引上被调用:那些从来没被赋 ...
- 百度地图JavaScript API经纬度查询-MAP
百度地图JavaScript API经纬度查询-MAP-ABCDEFGHIJKMHNOPQRSTUVWXYZ: 搜索:<input type="text" size=&quo ...
- [Javascript + rxjs] Introducing the Observable
In this lesson we will get introduced to the Observable type. An Observable is a collection that arr ...
- [RxJS] Add debug method to Observable in TypeScript
Observable.prototype.debug = function(message: any) { return this.do( (next) => { if(!environment ...
- [Javascript + rxjs] Simple drag and drop with Observables
Armed with the map and concatAll functions, we can create fairly complex interactions in a simple wa ...
- android 平台 java和javascript 通信问题 A WebView method was called on thread 'JavaBridge'.
java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge ...
- [RxJS] Implement the `map` Operator from Scratch in RxJS
While it's great to use the RxJS built-in operators, it's also important to realize you now have the ...
- [RxJS] Use RxJS concatMap to map and concat high order observables
Like switchMap and mergeMap, concatMap is a shortcut for map() followed by a concatAll(). In this le ...
随机推荐
- HTML -- 元素和属性
HTML -- 元素 HTML元素是从开始标签到结束标签之间的代码,如: <!-- 加粗标签 --> <b>一些元素</b> <!-- 换行 --> & ...
- Instruments --- 内存泄露
虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露还是可能存在.所以了解原理很重要. 这里讲述在没有ARC的情况下,如何使用Instruments来查找程序中的内存泄露, ...
- FFMPEG视音频编解码零基础学习方法-b
感谢大神分享,虽然现在还看不懂,留着大家一起看啦 PS:有不少人不清楚“FFmpeg”应该怎么读.它读作“ef ef em peg” 0. 背景知识 本章主要介绍一下FFMPEG都用在了哪里(在这里仅 ...
- PPT2010小技巧 教你如何快捷抠图
相信不少人在做PPT时,都有想插入个漂亮个性图案的想法,但是往往手头上的图片都不太令人满意,需要“裁剪”一下才能达不到自己想要的效果.这时大部分人可能会PS,但是相比起今天要分享给大家的方法,步骤就显 ...
- 模型 - 视图 - 控制器(MVC)详解
模型视图控制器(MVC)一个相当实用且十分流行的设计模式.作为一位称职码农,你不可能没听说过吧. 不幸的是它难以让人理解. 在本文中,我将给出我认为是MVC的最简单的解释,以及为什么你应该使用它. 什 ...
- 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)
[题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...
- iOS开发UI篇—UITableviewcell的性能问题
iOS开发UI篇—UITableviewcell的性能问题 一.UITableviewcell的一些介绍 UITableView的每一行都是一个UITableViewCell,通过dataSource ...
- Linux平台下Java调用C函数
JNI是Java native interface的简写,可以译作Java原生接口.Java可以通过JNI调用C/C++的库,这对于那些对性能要求比较高的Java程序无疑是一个 福音. 使用JNI也是 ...
- [OJ] Permutation Index
LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...
- 关于PowerBuilder 9.0中如何修改项目工程名字
关于PowerBuilder 9.0中如何修改项目工程名字,首先要找到三个文件,xxx.pbl.xxx.pbt.xxx.pbw这三个文件,为何要找这个三号个文件呢? 因为在使用PowerBuilder ...