[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 ...
随机推荐
- iOS - 打电话, 发短信
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- Android使用pull解析xml
一.理论准备 Pull解析器的运行方式与 SAX 解析器相似.它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件.跟SAX不同的是, ...
- ECshop 在迁移到 PHP7 时遇到的兼容性问题
在 PHP7 上安装 ECShop V2.7.3时,报错! Deprecated: Methods with the same name as their class will not be cons ...
- just so you're clear
The Google Resume的第一句话是: Just so you're clear: it was not my idea to give a talk to Microsoft Resear ...
- Linux守护进程daemon
守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常常在系统引导装入时启 ...
- [置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)
在c++中我们可以多继承来实现代码复用和封装使程序更加简练.在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(catego ...
- pcDuino无显示器刷机与使用
准备工作: pcduino : 点此购买 一个可用的并且启用了DHCP的有线路由器. 连接pcduino与路由器的网线一根. 至少1张4G microSD卡,如果内存卡不大,可以用内存卡刷内核,用u盘 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)
转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...
- PowerDesigner使用总结 转
PowerDesigner使用总结 (友情提示:本博文章欢迎转载,但请注明出处:陈新汉,http://www.blogjava.net/hankchen)一.使用PowerDesigner生成HTML ...
- Selenium WebDriver + Grid2 + RSpec之旅(五)---面向对象设计用例
Selenium WebDriver + Grid2 + RSpec之旅(五) ----面向对象设计用例 前几节讲了怎么一步一步的从零开始到编写出一个简单的测试用例,这一节将要讲一下怎么让测试用例变得 ...