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. 10个最实用的Linux命令

    收集了一些对于Linux新手最基本但最有用的Linux命令.你完全可以键入这些命令来管理你的服务器.这些命令对于学习vps或服务器管理的新手最为简便.1.List命令 ls -a //列出所有文件 l ...

  2. Sicily 1194. Message Flood

    题目地址:1194. Message Flood 思路: 不区分大小写,先全部转化为小写,用stl提供的函数做会很方便. 具体代码如下: #include <iostream> #incl ...

  3. MFC 点击改变文本 加法计时器 伸缩窗体

    1.添加所需控件,设置ID:labNum1,txtNum1.txtNum2.txtNum3.btnAdd,并将labNum1的属性Notify->true(控件在被单击或双击时可以发送消息) 2 ...

  4. 对于js原型和原型链继承的简单理解(第二种,对象冒充)

    关键代码    this.parent = Cat;    this.parent.apply(this); function Cat(){ this.climb = function(){ aler ...

  5. C# 汉子增加UTF-8头

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Conv ...

  6. android环境搭配 运行android sdk manager时出现错误问题解决

    本来前几天已经搭配好android环境开发(eclipse+android sdk+jdk),也已经运行成功了.但是最近因为出现了一些问题,所以把前面搭配好的环境都卸载了.重新搭配的时候发现在运行 s ...

  7. smarty练习: 设置试题及打印试卷

    数据库表格:shiti, shititimu, timu, kemu, xuanxiang 根据科目设置一个可以添加试题的页面:(如下图) 具体的题目从数据库中查出并形成一张试卷的形式 考试试题设置: ...

  8. 神奇的match和replace

    源自跟奈落大叔的讨论,PHP和JavaScript的比较. 正则: 先说几个正则写法: () 选择匹配一组, (?:) 降低 () 的优先级, .*? 和 .+? ,阻止 . 和 + 的贪婪. 还有一 ...

  9. JNI学习&使用过程中的错误

    Part 1 Ubuntu下JNI的简单使用: http://blog.csdn.net/fengqiaoyebo2008/article/details/6210499 Part 2 在eclips ...

  10. SQL SERVER 自带系统存储过程分类

    目录存储过程 用于实现 ODBC 数据字典功能,并隔离 ODBC 应用程序以使其不受基础系统表更改的影响. 变更数据捕获存储过程 用于启用.禁用.或报告变更数据捕获对象. 游标存储过程 用于实现游标变 ...