[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,让他变得很强大,也 ...
随机推荐
- Instruments --- 内存泄露
虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露还是可能存在.所以了解原理很重要. 这里讲述在没有ARC的情况下,如何使用Instruments来查找程序中的内存泄露, ...
- js带缩略图的图片切换效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- sap中用函数增加断点(break point)
如果在增强程序中,每次调试都要去程序里面设置断点很麻烦,为了解决这个问题,可以用下面的两个方法: 1: if sy-uname eq 'XXXX' "XXX 为账号名字 break ...
- Ubuntu配置apache
http://blog.csdn.net/ljchlx/article/details/21978431 http://www.2cto.com/os/201110/107283.html
- 【网络流24题】 No.10 餐巾计划问题 (线性规划网络优化 最小费用最大流)
[题意] 一个餐厅在相继的 N 天里, 每天需用的餐巾数不尽相同. 假设第 i 天需要 ri 块餐巾(i=1,2,-, N). 餐厅可以购买新的餐巾,每块餐巾的费用为 p 分:或者把旧餐巾送到快洗部, ...
- SlidingMenu+ViewPager实现侧滑菜单效果
先简单介绍下SlidingMenu和ViewPager. ViewPager就是一个官方提供的多页面滑动组件,需要一个适配器来构建多个页面. 先来看看ViewPager对应的基本适配器PageAdap ...
- Tomcat J2ee 发布步骤
1.找到要发布的工程,并发布到本地tomcat下,测试完全没有问题,找到tomcat下webapps下 并找到该工程,进入该工程目录,全选添加到 drivingSchool.zip 或 drivi ...
- 利用CMake生成动态或静态链接库工程
install解释: TARGETS版本的install命令 install(TARGETS targets... [EXPORT <export-name>] [[ARCHIVE|LIB ...
- INFORMATION_SCHEMA.INNODB_TRX 详解
从192.168.11.186 上登录 192.168.11.185 数据库: root 13246 547 0 13:39 pts/1 00:00:00 mysql -uroot -px xxxxx ...
- Android之最简单的ImageView加边框方法
转自:http://www.th7.cn/Program/Android/201301/120345.shtml 通常情况下,如果我们要给ImageView加上边框,比如宽为3dp的灰色框,是自己定义 ...