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的更多相关文章

  1. [Javascript] The Array map method

    One very common operation in programming is to iterate through an Array's contents, apply a function ...

  2. Javascript中Array.prototype.map()详解

    map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数.callback 每次执行后的返回值组合起来形成一个新数组. callback 函数只会在有值的索引上被调用:那些从来没被赋 ...

  3. 百度地图JavaScript API经纬度查询-MAP

    百度地图JavaScript API经纬度查询-MAP-ABCDEFGHIJKMHNOPQRSTUVWXYZ: 搜索:<input type="text" size=&quo ...

  4. [Javascript + rxjs] Introducing the Observable

    In this lesson we will get introduced to the Observable type. An Observable is a collection that arr ...

  5. [RxJS] Add debug method to Observable in TypeScript

    Observable.prototype.debug = function(message: any) { return this.do( (next) => { if(!environment ...

  6. [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 ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. csu 10月 月赛 F 题 ZZY and his little friends

    一道字典树异或的题,但是数据比较水,被大家用暴力给干掉了! 以前写过一个类似的题,叫做the longest xor in tree: 两个差不多吧! 好久没写字典树了,复习一下! 代码: #incl ...

  2. c语言命名规则 [转载]

    C语言变量名命名规则 一.程序风格:         1.严格采用阶梯层次组织程序代码:         各层次缩进的分格采用VC的缺省风格,即每层次缩进为4格,括号位于下一行.     要求相匹配的 ...

  3. android studio 偶记

    修改项目名称 如果仅仅改了文件夹的名字,则会出现引用问题,相应的如下文件都要做相应的修改: 1. package name 要做相应调整 2. settings.gradle ,中要修改相应的moda ...

  4. html表格cell合并插件

    数据展示时需要合并部分数据自己写了一个简单插件 合并前: 合并后: 调用示例: var trs = $('table#dataList tbody tr').not('#demo').get(); v ...

  5. VirtualBox设置共享文件夹和镜像访问的方法

    VirtualBox设置共享文件夹和镜像访问的方法 virtualBox是一款虚拟机软件,可以在该软件上安装各类的操作系统,至于如何安装请参见另外一篇经验<如何使用VirtualBox安装win ...

  6. wifi测试相关(iwconfig,WPA Supplicant用法)

    iwconfig用法 1.打开无线网卡电源 iwconfig wlan0 txpower no 2.列出区域内的无线网络 iwconfig wlan0 scan 3.假设要连接到网络myhome(即e ...

  7. linux 下的sublime

    Sublime Text 2 的安装 : 在官方网站下载Linux版本  Or  执行 #  wget http://c758482.r82.cf2.rackcdn.com/Sublime%20Tex ...

  8. 切换Oracle数据库实例

    如果多个实例,需要切换可以采用以下命令: export ORACLE_SID=SID

  9. glsl-BufferObject- change

    修改其值的最快方式: 创建: Mutable Storage To create mutable storage for a buffer object, you use this API: void ...

  10. 2D游戏编程2--windows高级编程

      windows应用程序布局 编译流程 响应菜单事件消息 菜单消息处理实例: LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wpar ...