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. NYOJ 16 矩形嵌套(动态规划)

    矩形嵌套 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 4   描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅 ...

  2. http发送post请求

    package com.j1.soa.resource.member.oracle.service; import java.io.BufferedReader; import java.io.IOE ...

  3. 自定义View-6 状态按钮 滑动 点击

    View public class SwitchButton extends View implements OnClickListener, OnTouchListener {     privat ...

  4. 理解prototype、proto和constructor的三角关系

    javascript里的关系又多又乱.作用域链是一种单向的链式关系,还算简单清晰:this机制的调用关系,稍微有些复杂:而关于原型,则是prototype.proto和constructor的三角关系 ...

  5. javascript 闭包的理解

    1 需要明白概念: 执行环境 变量对象,活动对象 作用域,作用域链 闭包 垃圾处理机制 闭包陷阱

  6. 一个简单C程序的汇编代码分析

    几个重要的寄存器 eip - 用于存放当前所执行的指令地址 esp - 栈(顶)指针寄存器 ebp - 基址(栈底)指针寄存器 简单的C程序 int g(int x) { ; } int f(int ...

  7. YUI 之yui.js

    一.构造函数直接返回一个对象,避免调用时出错. Function Fvar F = function () { var f = this; instanceOf = function (o, type ...

  8. 推荐Mac软件Alfred

    实在忍不住推荐这个软件了, 身边的朋友们逐渐都在使用Mac OS了,每次我都会推荐Alfred这个软件.推荐来推荐去挺蛮烦的,干脆写篇文章, 下次有朋友新入手Macbook,我就直接附送本文章链接一枚 ...

  9. Edusoho 的 Arale validator使用说明

    1.js控制器文件开端 var Validator = require('bootstrap.validator'); require('common/validator-rules').inject ...

  10. gets和从键盘输入换行符

    i was wrong! 虽然setbuf可以让程序自己管理缓冲,但是像getchar,gets这些标准IO函数还是要通过隐藏的stdin进行操作,而stdin是啥呢?还是一个FILE*,而FILE* ...