[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 ...
随机推荐
- MDN > Web technology for developers > HTTP
The Hypertext Transfer Protocol (HTTP) is an application-layer protocol for transmitting hypermedia ...
- Win7系统Matlab2013a安装.m文件不自动关联到MATLAB.exe解决方法
1.在matlab命令行中输入以下代码: cwd=pwd; cd([matlabroot '\toolbox\matlab\winfun\private']); fileassoc('add',{'. ...
- Network Wars
zoj2676:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 题意:给出一个带权无向图 ,每条边e有一个权 .求将点 ...
- java api如何获取kafka所有Topic列表,并放置为一个list
kafka内部所有的实现都是通过TopicCommand的main方法,通过java代码调用API,TopicCommand.main(options)的方式只能打印到控制台,不能转换到一个list. ...
- AFN演示
- ANDROID_MARS学习笔记_S03_005_Geocoder、AsyncTask
一.代码1.xml(1)AndroidManifest.xml <uses-permission android:name="android.permission.ACCESS_FIN ...
- Python3整理文件
晚上打算把播放器下载下来的音乐拷贝到mp3里边,但是它是如下形式存放的,相当头痛…… 作为程序员,想到使用python来遍历这个目录,并将有大于限制的音乐文件拷贝到指定目录,相关实现代码如下: # a ...
- qt容器在并发时需要注意的地方
最近用tbb和qt写了一个延时摄影后期控制镜头的工具,主要就是扫描目录下所有图片,按照给定参数截取图片中某区域并另存,模拟镜头摆动. 扫描后的图片路径保存在qlist内,作为只读数据,交由tbb的pa ...
- C++ const&的一个特性
最近在搜索类似scope exit的实现时,除了发现已经有人向标准委员会提出意见,还得到一些意外的C++特性,这个特性一直都存在,而且很有趣 http://herbsutter.com/2008/01 ...
- [pod install] error: cannot open .git/FETCH_HEAD: Permission denied
pod installAnalyzing dependencies[!] Pod::Executable pull error: cannot open .git/FETCH_HEAD: Permis ...