[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,让他变得很强大,也 ...
随机推荐
- 常见内部函数----Python
In [10]: test = [1,2,"yes"] ---append(x) 追加到链尾 In [11]: test.append(1) In [12]: test Out[1 ...
- java MYSQL做分页
MySql中查询语句实现分页功能 语句: select * from 表名 where 条件 limit 要找第几页,每页多少行; import java.util.*; import java.sq ...
- 定位 - CoreLocation - 区域报警
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...
- Re-installation failed due to different application signatures./package name has exist
http://www.cnblogs.com/qianxudetianxia/archive/2011/04/09/2010468.html
- gdb调试高级用法
Linux下进程崩溃时定位源代码位置 如何在调试内核时,同时可以调试应用程序的做法: (cskygdb) c Continuing. ^C Program received signal SIGINT ...
- [收藏转贴]构建RESTful风格的WCF服务
RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台都支持 HTTP 请求. RESTf ...
- 启动redis出现Creating Server TCP listening socket *:6379: bind: No such file or directory
E:\redis>redis-server.exe redis.windows.conf [8564] 10 Oct 20:00:36.745 # Creating Server TCP lis ...
- Eclipse中如何恢复已删除文件
[转自] http://zhangjunhd.blog.51cto.com/113473/23092
- Android调试时, "adb devices"命令提示 adb server is out of date. killing...
C:\Users\xxxx>adb devicesadb server is out of date. killing... 查看端口, 发现被占用 C:\Users\xxxx>adb n ...
- C#设计模式(23种设计模式)
转自:http://www.cnblogs.com/abcdwxc/archive/2007/10/30/942834.html 创建型: 1. 单件模式(Singleton Patte ...