RxJS .zip and .switchLatest
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.7/rx.all.js"></script>
<button id="validation">Click me</button>
<textarea id='ta_validation'></textarea>
<textarea id='ta_change'></textarea>
<textarea id='ta_result'></textarea>
CSS
textarea {
font-family: "Courier New", monospace;
font-size: 12px;
width: 100%;
height: 120px;
}
JavaScript
var ta_validation = document.getElementById('ta_validation');
var ta_change = document.getElementById('ta_change');
var ta_result = document.getElementById('ta_result');
function emits(who, who_) {
return function(x) {
who.innerHTML = [who.innerHTML, who_ + " emits " + JSON.stringify(x)].join("\n");
};
}
var S = new Rx.Subject();
var source = Rx.Observable.fromEvent(document.getElementById('validation'), 'click')
.map(function(){return 'click'})
.do(emits(ta_validation, 'Source'));
S.take(2)
.do(emits(ta_change, 'Take 2'))
.subscribe(
function(){console.log("Take 2 emits");},
function(){console.log("Take 2 errors");},
function(){console.log("Take 2 completes");}
);
S
.do(emits(ta_result, 'Take all'))
.subscribe(function(){console.log("Take all emits");});
source.subscribe(S);
//changeEvent$.subscribe(function() {});
//filteredValidation$.subscribe(function() {});