最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RxJS flatMapLatest is not a function - Stack Overflow

programmeradmin7浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 22

In RxJS 5, flatMapLatest has been renamed to switchMap.

var suggestions = distinct.switchMap(searchWikipedia);
发布评论

评论列表(0)

  1. 暂无评论