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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 九、Rxjs请求对Observable进行封装

    1.引入 Http.Jsonp.Rxjs 三个模块 2.请求中添加一个 .map(res => res.json) 问题 1.Property 'map' does not exist on t ...

  4. rxjs简单的Observable用例

    import React from 'react'; import { Observable } from 'rxjs'; const FlowPage = () => { const onSu ...

  5. [rxjs] Creating An Observable with RxJS

    Create an observable var Observable = Rx.Observable; var source = Observable.create(function(observe ...

  6. [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 ...

  7. rxjs——subject和Observable的区别

    原创文章,转载请注明出处 理解 observable的每个订阅者之间,是独立的,完整的享受observable流动下来的数据的. subject的订阅者之间,是共享一个留下来的数据的 举例 这里的cl ...

  8. Angular快速学习笔记(4) -- Observable与RxJS

    介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...

  9. Angular学习笔记—RxJS与Observable(转载)

    1. Observable与观察者模式的关系 其实这里讲的Observable就是一种观察者模式,只不过RxJS把Observable结合了迭代模式以及附件了很多的operator,让他变得很强大,也 ...

随机推荐

  1. 第 4 章 多例模式【Multition Pattern】

    以下内容出自:24种设计模式介绍与6大设计原则 这种情况有没有?有!大点声,有没有? 有!,是,确实有,就出现在明朝,那三国期间的算不算,不算,各自称帝,各有各的地盘,国号不同.大家还记得那首诗< ...

  2. c/c++动态分配内存和malloc的使用

    c/c++动态分配内存  为什么需要动态分配内存 ---很好的解决的了传统数组的4个缺陷 动态内存分配举例 ---动态数组的构造 使用动态数组的优点:    1. 动态数组长度不需要事先给定: 2. ...

  3. python学习之--内置函数:

    Python内置函数: Python内置了很多有用的函数,我们可以直接调用.要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs,只有一个参数. 1. 内置函数调用之--abs()函数: ...

  4. 重燃你的PHP安全分析之火

    关于脚本安全这个话题好像永远没完没了,如果你经常到国外的各种各样的bugtraq上,你会发现有一半以上都和脚本相关,诸如SQL injection,XSS,Path Disclosure,Remote ...

  5. 【网络流24题】 No.22~24

    接下来几题就写写题解吧.不是很想打了. 22. 输入文件示例input.txt4 21 2 7 36 5 8 37 8 10 59 6 13 9 输出文件示例output.txt17 最长不相交路径. ...

  6. 主线程中有多个handler的情况

    工作中遇到了这么一种情况,有两个视图,都需要开启异步任务从服务器获取数据,每个view中创建一个Handler,注册到异步任务中去,当异步任务从服务器获取数据出错,或者出现io异常或者http协议异常 ...

  7. Redundant Call to Object.ToString()

    Redundant Call to Object.ToString() The + operator for string is overloaded to call String.Concat pa ...

  8. WCF的配置文件中的要素

    Windows Communication Foundation Configuration Schema

  9. statspack系列6

    原文:http://jonathanlewis.wordpress.com/2006/12/27/analysing-statspack-6/ 作者:Jonathan Lewis 下面是一段时间以前网 ...

  10. 汉企C#面向对象——继承Practice

    class Dianqi //创建电器类:父类 { private string _Dianqimingzi; public string Dianqimingzi { get { return _D ...