When doing search function, you always need to consider about the concurrent requests.

AEvent ----(6s)---> AResult

------(100ms)-------

BEvent -----(1s)---> BResult

It means A event needs to take 6 seconds to get the result, but B only need 1 second, in the between there is 100ms.

So B result will appear on the DOM first, but later will be overwritten by A result once A finished.

To overcome this problem, we can use RxJS:

class HelpSearchCtrl {
constructor(HelpSearchService, $scope, $log) {
this.HelpSearchService = HelpSearchService;
this.searchTerm = null;
this.$log = $log;
this.$scope = $scope; let listener = Rx.Observable.create( (observe)=>{ this.$scope.$watch( ()=>{
return this.searchTerm;
}, ()=>{
observe.onNext(this.searchTerm);
})
})
.debounce(500)
.flatMapLatest( (searchTerm)=> {
return Rx.Observable.fromPromise(this.doSearch(searchTerm));
}).subscribe(); this.$scope.$on('$destory', ()=>{
this.$log.debug('cancelled!');
listener.dispose();
})
} doSearch(searchTerm) {
return this.HelpSearchService.searchForContent(searchTerm);
}
} const clmHelpSearchDirective = () => {
return {
restrict: 'EA',
scope: {},
replace: true,
template: require('./help-search.html'),
controller: HelpSearchCtrl,
controllerAs: 'vm',
bindToController: true
}
}; export default (ngModule)=> {
ngModule.directive('clmHelpSearch', clmHelpSearchDirective);
}

[AngularJS + RxJS] Search with RxJS的更多相关文章

  1. AngularJS filter:search 是如何匹配的 ng-repeat filter:search ,filter:{$:search},只取repeat的item的value 不含label

    1.  filter可以接收参数,参数用 : 进行分割,如下: {{ expression | filter:argument1:argument2:... }} 2.   filter参数是 对象 ...

  2. [RxJS + AngularJS] Sync Requests with RxJS and Angular

    When you implement a search bar, the user can make several different queries in a row. With a Promis ...

  3. [RxJS] Introduction to RxJS Marble Testing

    Marble testing is an expressive way to test observables by utilizing marble diagrams. This lesson wi ...

  4. [RxJS] Split an RxJS Observable into groups with groupBy

    groupBy() is another RxJS operator to create higher order observables. In this lesson we will learn ...

  5. [RxJS] Split an RxJS observable conditionally with windowToggle

    There are variants of the window operator that allow you to split RxJS observables in different ways ...

  6. [RxJS] Split an RxJS observable with window

    Mapping the values of an observable to many inner observables is not the only way to create a higher ...

  7. RxJS v6 学习指南

    为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...

  8. 构建流式应用—RxJS详解[转]

    目录 常规方式实现搜索功能 RxJS · 流 Stream RxJS 实现原理简析 观察者模式 迭代器模式 RxJS 的观察者 + 迭代器模式 RxJS 基础实现 Observable Observe ...

  9. RxJS入门

    一.RxJS是什么? 官方文档使用了一句话总结RxJS: Think of RxJS as Lodash for events.那么Lodash主要解决了什么问题?Lodash主要集成了一系列关于数组 ...

随机推荐

  1. ssh远程登录linux live系统

    要想ssh远程登录,须要准备两件事:配置同网段IP和开启SSH服务. 因为live系统没有IP,所以首先须要配置IP. 我的live系统是在虚拟机上启动的,宿主IP为192.168.230.1,liv ...

  2. Java为什么使用连接池

    一.简介 动态Web站点往往用数据库存储的信息生成Web页面,每一个页面请求导致一次数据库访问.连接数据库不仅要开销一定的通信和内存资源,还必须完成用户验证.安全上下文配置这类任务,因为往往成为最为耗 ...

  3. SqlServer 数据库进行定时自动的执行脚本

    需求:数据库进行自动执行脚本操作,有些数据库操作就不需要写程序来定时执行,完全可以交给我们SqlServer客户端代理来做,下面的步骤,我是结合前一篇文章的SqlServer不同服务器数据库之间数据传 ...

  4. java基础day7

    1/匿名类对象:创建类的对象是匿名的. 比如说new Circle():就是一个匿名类对象. 匿名类对象只能使用一次. 2/形参:声明方法时,方法小括号内的参数 实参:调用方法是,实际传入的参数的值 ...

  5. CSS特殊性

    样式的优先级取决于特殊性,特殊性为0,0,0,0 Ø每个元素或伪元素选择器贡献特殊性为 0,0,0,1 Ø每个类.伪类或者属性选择器的特殊性为 0,0,1,0 Ø每个ID选择器的特殊性为 0,1,0, ...

  6. 表单控件之select

    一.表单控件之表单 1.依次获取表单里的所有控件: for (i = 0; i < document.getElementById("formName").length; i ...

  7. php 图片上传预览(转)

    网上找的图片上传预览: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  8. Python新手学习基础之初识python——与众不同1

    Python是什么? 首先我们先简单介绍下python这门语言,Python是一种解释性的脚本语言,它不需要像C/C++那样先编译再执行,也不像JS那样可以在浏览器上直接执行.它为我们提供的基础代码库 ...

  9. C 语言字符 和字符串输出

    int main(void){ char ch; char str[80]; printf("Input a string: ");    //先输入字符串 gets(str);/ ...

  10. 装饰者模式 - OK

    装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活.  装饰者模式隐含的是通过一条条装饰链去实现具体对象,每一条装饰链都始于一个Compon ...