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. Android中JNI编程的那些事儿(1)

    转:Android中JNI编程的那些事儿(1)http://mobile.51cto.com/android-267538.htm Android系统不允许一个纯粹使用C/C++的程序出现,它要求必须 ...

  2. 在windows下创建.gitignore文件

    1.使用另存为的方式   2.在win7下,文件名输入 ”.gitignore.“ http://hbiao68.iteye.com/blog/2055496 http://lyhopq.github ...

  3. [原博客] POJ 1067 取石子游戏

    题目链接有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者 ...

  4. Linux下fork()、vfork()、clone()和exec()的区别

    转自Linux下fork().vfork().clone()和exec()的区别 前三个和最后一个是两个类型.前三个主要是Linux用来创建新的进程(线程)而设计的,exec()系列函数则是用来用指定 ...

  5. java向文件写数据的3种方式

    下边列举出了三种向文件中写入数据的方式,当然还有其他方式,帮助自己理解文件写入类的继承关系.类的关系: file->fileoutputstream->outputstreamWriter ...

  6. android layout 属性大全

    第一类:属性值为true可false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:la ...

  7. 开发神器之--Sublime Text

    原来还有这么个神器啊,忍痛丢掉了notepad++,投入她的怀抱!! 使用和介绍就不写了,大牛们已经整理了. 官网:http://www.sublimetext.com/ 入门及心得:http://w ...

  8. 使用socket.io打造公共聊天室

    最近的计算机网络课上老师开始讲socket,tcp相关的知识,当时脑袋里就蹦出一个想法,那就是打造一个聊天室.实现方式也挺多的,常见的可以用C++或者Java进行socket编程来构建这么一个聊天室. ...

  9. ubuntu相关软件合集(持续更新中)

    本人使用的是Ubuntu-Kylin14.04,自带了日历.输入法.优客助手等易于上手的应用.省的每次安装完原生的系统再麻烦的安装,下面介绍默认应用外的相关常用软件: 一.Keylock Applic ...

  10. JavaScript高级程序设计48.pdf

    设备中的键盘事件 任天堂Wii等设备可以通过键码知道用户按下了哪个键 复合事件 复合事件是DOM3级事件新添加的一类事件,用于处理IME的输入序列.IME(Input Method Editor,输入 ...