[AngularJS + RxJS] Search with RxJS
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的更多相关文章
- AngularJS filter:search 是如何匹配的 ng-repeat filter:search ,filter:{$:search},只取repeat的item的value 不含label
1. filter可以接收参数,参数用 : 进行分割,如下: {{ expression | filter:argument1:argument2:... }} 2. filter参数是 对象 ...
- [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 ...
- [RxJS] Introduction to RxJS Marble Testing
Marble testing is an expressive way to test observables by utilizing marble diagrams. This lesson wi ...
- [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 ...
- [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 ...
- [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 ...
- RxJS v6 学习指南
为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...
- 构建流式应用—RxJS详解[转]
目录 常规方式实现搜索功能 RxJS · 流 Stream RxJS 实现原理简析 观察者模式 迭代器模式 RxJS 的观察者 + 迭代器模式 RxJS 基础实现 Observable Observe ...
- RxJS入门
一.RxJS是什么? 官方文档使用了一句话总结RxJS: Think of RxJS as Lodash for events.那么Lodash主要解决了什么问题?Lodash主要集成了一系列关于数组 ...
随机推荐
- HTML基础总结<链接>
HTML 超链接(链接) HTML使用标签 <a>来设置超文本链接. 超链接可以是一个字,一个词,或者一组词,也可以是一幅图像,您可以点击这些内容来跳转到新的文档或者当前文档中的某个部分. ...
- 关于html控件和服务器控件摁回车后提交按钮的问题
今天做项目用到,项目是一个洗车系统,刷卡后在焦点出自动触发回车键事件,如,一个文本框,把焦点放入,刷一下卡,文本框自动获取卡号,同时触发回车事件,(就像银行办卡一样),发现刷卡后页面刷新后并没有执行按 ...
- Xcode简易基础篇,以新手角度去操作
声明:此Newlife XCode非Mac的XCode,避免误会. 日常用的Newlife X组件的相关资源,不限于XCode,只是以XCode组件为主: 1.QQ群:1600800 2.博客 : h ...
- 解决MacOS Terminal打开慢的问题
用了Mac有一段时间了,突然发现Terminal打开奇慢,每次打开都显示logining...,打开大概要个五六秒的时间,以前打开都是瞬间打开的啊,这对于我们这种追求速度的程序员怎么受的了呢.开始一直 ...
- Sudoku Killer
算法:深搜 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份 ...
- ubuntu 快捷键和安装知识知识
本文节选自“The Official Ubuntu Book, 7th Edition.pdf” 快捷键部分直接引用原书中图片. Linux Folders Learning Unity Keyboa ...
- Sicily 1156. Binary tree
题目地址:1156. Binary tree 思路: 如何愉快地前序输出呢,要在存储数据的时候根据位置来存放数据! 一开始被自己蠢哭,一直以为第一个输入的就是根结点(例子的祸害呀啊啊啊!!!!),结果 ...
- Guava API学习之Multimap
相信大家对Java中的Map类及其之类有大致的了解,Map类是以键值对的形式来存储元素(Key->Value),但是熟悉Map的人都知 道,Map中存储的Key是唯一的.什么意思呢?就是假如我们 ...
- dedecms 织梦ping服务插件 最新破解可用版
dedecms 织梦ping服务插件 最新破解可用版 ping_gbk.xml <module> <baseinfo> name=ping服务 team=井哥 time=20 ...
- python django学习资料网站
python module 模块 https://docs.python.org/2.7/py-modindex.html django框架例子 https://docs.djangoproject. ...