[Javascript + rxjs] Introducing the Observable
In this lesson we will get introduced to the Observable type. An Observable is a collection that arrives over time. Observables can be used to model events, asynchronous requests, and animations. Observables can also be transformed, combined, and consumed using the Array methods we learned in the previous lessons. We can write powerful and expressive asynchronous programs using the few simple methods we've learned so far.
Here is an example how to handle events normally:
var button = document.getElementById('button');
var handler = function(e){
console.log('clicked');
};
button.addEventListener('click', handler);
So when fire the click event, in the console will log 'clicked'.
If we want to remove the event listener:
var button = document.getElementById('button');
var handler = function(e){
console.log('clicked');
button.removeEventListener('click', handler);
};
button.addEventListener('click', handler);
Now let see how can we achieve the same effect by using observable.
var Observable = Rx.Observable; //Create click events by Observable
var clicks = Observable.fromEvent(button, 'click'); //Then we are able to use forEach, concatAll, map, fliter function
clicks.forEach(function() {
console.log('clicked');
});
How to remove the event listener:
var Observable = Rx.Observable; //Create click events by Observable
var clicks = Observable.fromEvent(button, 'click'); //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 = clicks.forEach(function() {
console.log('clicked');
subscription.dispose();
});
forEach method actually has three callback function which are onNext, onError, onCompleted:
var Observable = Rx.Observable; //Create click events by Observable
var clicks = Observable.fromEvent(button, 'click'); //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 = clicks.forEach(function onNext() {
console.log('clicked');
subscription.dispose();
}, function onError() {
console.log('error');
}, function onCompleted() {
console.log('complete');
});
[doc](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/subscribe.md)
[Javascript + rxjs] Introducing the Observable的更多相关文章
- [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 Observa ...
- [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 ...
- 九、Rxjs请求对Observable进行封装
1.引入 Http.Jsonp.Rxjs 三个模块 2.请求中添加一个 .map(res => res.json) 问题 1.Property 'map' does not exist on t ...
- rxjs简单的Observable用例
import React from 'react'; import { Observable } from 'rxjs'; const FlowPage = () => { const onSu ...
- [rxjs] Creating An Observable with RxJS
Create an observable var Observable = Rx.Observable; var source = Observable.create(function(observe ...
- [RxJS] Subject: an Observable and Observer hybrid
This lesson teaches you how a Subject is simply a hybrid of Observable and Observer which can act as ...
- rxjs——subject和Observable的区别
原创文章,转载请注明出处 理解 observable的每个订阅者之间,是独立的,完整的享受observable流动下来的数据的. subject的订阅者之间,是共享一个留下来的数据的 举例 这里的cl ...
- Angular快速学习笔记(4) -- Observable与RxJS
介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...
- Angular学习笔记—RxJS与Observable(转载)
1. Observable与观察者模式的关系 其实这里讲的Observable就是一种观察者模式,只不过RxJS把Observable结合了迭代模式以及附件了很多的operator,让他变得很强大,也 ...
随机推荐
- java通过jni方式获取硬盘序列号(windows,linux)
linux系统java通过jni方式获取硬盘序列号 http://blog.csdn.net/starter110/article/details/8186788 使用jni在windows下读取硬盘 ...
- UVA 11426 GCD Extrme (Ⅲ)
给定一个整数N(1<N<=4000000)的整数求∑GCD(i,j)i=1,2,3....j-1,2<=j<=n的值.参考了一下网上的题解,复述一下我理解后的思路,加深理解: ...
- xbmc
XBMC是一个优秀的自由和开源的(GPL)媒体中心软件.XBMC最初为Xbox而开发,可以运行在Linux.OSX.Windows.Android4.0系统.XBMC能够播放几乎所有流行的音频和视频格 ...
- Win7资源管理器已停止工作——StackHash_6c37,R6205错误
2013-9-20 此问题由来已久,截图及"问题签名"如下: 问题签名: 问题事件名称: BEX64 应用程序名: Explorer.EXE 应用程序版本: 6.1.7601. ...
- Hibernate:组合模式解决树的映射
树经常用来展示目录结构,那么在Hibernate中怎样解决树的映射问题呢? 先来看一个分销商的树形结构的例子 所有分销商 东北区 辽宁省 沈阳医药 吉林省 华北区 北京市 北京医药 河北省 华南区 那 ...
- Tomcat J2ee 发布步骤
1.找到要发布的工程,并发布到本地tomcat下,测试完全没有问题,找到tomcat下webapps下 并找到该工程,进入该工程目录,全选添加到 drivingSchool.zip 或 drivi ...
- Android事件分发详解(三)——ViewGroup的dispatchTouchEvent()源码学习
package cc.aa; import android.os.Environment; import android.view.MotionEvent; import android.view.V ...
- 【HDOJ】1059 Dividing
多重背包. #include <stdio.h> #include <string.h> #define mymax(a, b) (a>b) ? a:b ]; ]; vo ...
- linux 下 poll 编程
poll 与 select 很类似,都是对描述符进行遍历,查看是否有描述符就绪.如果有就返回就绪文件描述符的个数将.poll 函数如下: #include <poll.h> int pol ...
- C++运行字符编码于MSVC和GCC之间的区别
详细请参考这篇博文 http://blog.csdn.net/dbzhang800/article/details/7540905 运行字符编码就是指,当你源代码写下const char* p = & ...