Using RxJs
I am trying to reproduce a demo. But i am not getting any result. Instead getting error as : distinct.flatMapLatest is not a function
what is wrong i did here? do i required to add any library
here?
here is my try: but look in to demo to get real issue what is face.
$(document).ready(function(){
var $input = $('#input'),
$results = $('#results');
var keyups = Rx.Observable.fromEvent($input, 'keyup')
.map(e => e.target.value)
.filter(text => text.length > 2);
var throttled = keyups.throttle(500);
var distinct = throttled.distinctUntilChanged();
function searchWikipedia (term) {
return $.ajax({
url: '.php',
dataType: 'jsonp',
data: {
action: 'opensearch',
format: 'json',
search: term
}
}).promise();
}
var suggestions = distinct.flatMapLatest(searchWikipedia);
suggestions.subscribe(data => {
var res = data[1];
/* Do something with the data like binding */
$results.empty();
$.each(res, (_, value) => $('<li>' + value + '</li>').appendTo($results));
}, error => {
/* handle any errors */
$results.empty();
$('<li>Error: ' + error + '</li>').appendTo($results);
});
})
Live Demo
Using RxJs
I am trying to reproduce a demo. But i am not getting any result. Instead getting error as : distinct.flatMapLatest is not a function
what is wrong i did here? do i required to add any library
here?
here is my try: but look in to demo to get real issue what is face.
$(document).ready(function(){
var $input = $('#input'),
$results = $('#results');
var keyups = Rx.Observable.fromEvent($input, 'keyup')
.map(e => e.target.value)
.filter(text => text.length > 2);
var throttled = keyups.throttle(500);
var distinct = throttled.distinctUntilChanged();
function searchWikipedia (term) {
return $.ajax({
url: 'http://en.wikipedia/w/api.php',
dataType: 'jsonp',
data: {
action: 'opensearch',
format: 'json',
search: term
}
}).promise();
}
var suggestions = distinct.flatMapLatest(searchWikipedia);
suggestions.subscribe(data => {
var res = data[1];
/* Do something with the data like binding */
$results.empty();
$.each(res, (_, value) => $('<li>' + value + '</li>').appendTo($results));
}, error => {
/* handle any errors */
$results.empty();
$('<li>Error: ' + error + '</li>').appendTo($results);
});
})
Live Demo
Share Improve this question edited Mar 9, 2016 at 5:46 m59 43.8k14 gold badges121 silver badges139 bronze badges asked Mar 9, 2016 at 5:18 user2024080user2024080 5,10116 gold badges63 silver badges117 bronze badges1 Answer
Reset to default 22In RxJS 5, flatMapLatest
has been renamed to switchMap
.
var suggestions = distinct.switchMap(searchWikipedia);