I'm learning RxJS by reading this tutorial http://reactive-extensions.github.io/learnrx/
.
I have a hard time understanding the map method of Observable
. The Array
version of map
is really simple and straightforward. I have no idea about what exactly the map
means in case of a Observable
(and why does it have a alias named select
?!).
Here is what the documentation told me. Might not be helpful for most beginners...
Projects each element of an observable sequence into a new form by incorporating the element's index. This is an alias for the select method.
I don't understand map
in the context of event
.
For example, the code below works exactly what I expected. I thought of this piece of code as : "Listen to the click-event
from the event-stream of #btn
".
var btnClicks, observable;
btnClicks = Rx.Observable.fromEvent($('#btn'), "click");
observable = btnClicks.subscribe(function(e) {
console.log(e);
});
But what happens when it becomes to this??
var btn2Clicks, btnClicks, observable;
btnClicks = Rx.Observable.fromEvent($('#btn'), "click");
btn2Clicks = Rx.Observable.fromEvent($('#btn2'), "click");
observable = btnClicks.map(function(e) {
return btn2Clicks;
}).subscribe(function(e) {
console.log(e);
});
What I thought is use the map
to transform a collection of a click-event to another collection of event-collection.
The filter
is easy to understand, it just as the word filter
means, take the event only I interested, and skip others. But how about the map
in the context of event
? If it means 'transform a collection to another ones' just as the array version, why it still fires when #btn
clicked??
I mean I'v mapped it to another collections, now it's no longer a collection of click-event of #btn
but it's a new collection of something... But it still fires when #btn
clicked which not make sense for me.