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. AppStore 审核中文版 --- 程序员必看

    App Store审核指南中文版(2014.10.11更新) 2014-11-03 程序猿 苹果在9月3日对App Store审核指南进行了重大更新,新添加了扩展.HealthKit.HomeKit以 ...

  2. uva 1400 - "Ray, Pass me the dishes!"

    又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...

  3. 我是如何实用:before :after

    本文地址http://www.cnblogs.com/Bond/p/3972854.html 最近一直做移动端,没和IE6打交道了,瞬间感觉世界变美好了.移动端虽然还是各种坑,但是比起修复IE6那还是 ...

  4. 【BZOJ3439】 Kpm的MC密码 (TRIE+主席树)

    3439: Kpm的MC密码 Description 背景 想Kpm当年为了防止别人随便进入他的MC,给他的PC设了各种奇怪的密码和验证问题(不要问我他是怎么设的...),于是乎,他现在理所当然地忘记 ...

  5. 【POJ 3167】Cow Patterns (KMP+树状数组)

    Cow Patterns Description A particular subgroup of K (1 <= K <= 25,000) of Farmer John's cows l ...

  6. Swift与Objective-C中的闭包

    Swift Code: func makeIncrementor(forIncrement amount: Int) -> (() -> Int,() -> Int) { func ...

  7. 没做过编译器就是被人欺——从一道变态的i++题猜编译器的行为(表达式从左往右扫描,同一变量相互影响)

    首先不要被人蒙了,如果是这样,根本编译不过: int i=1; int b=i+++++i; printf("%d %d\n", b ,i); Mingw报错:error: lva ...

  8. java 包中的一款经典的singleton模式

    /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  9. Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释

    本文大部分参考自 http://blog.csdn.net/mayingcai1987/article/details/6200909 ,对原文中的讲解FLAG_NEW_TASK地方加了一些自己的观点 ...

  10. ASP.Net4.0中新增23项功能

    这篇文章介绍Visual Studio 2010 (ASP.Net 4.0)的新功能. 1.代码片段(Code Snippets): 代码段是预先开发的代码模板,可以节省我们对有关语法思考的时间.在V ...