This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.engine.rx.js
68 lines (52 loc) · 2.22 KB
/
app.engine.rx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(function($, _, Rx, Common, exports) {
exports.startEngine = startEngine
function startEngine() {
var searchInput = $('#search .controls input')
.onAsObservable('keyup')
.throttle(500)
.select(currentTargetValueOf)
.select($.trim)
.publish()
.refCount()
var searchInvalidInput = searchInput.where(_.isEmpty)
var searchValidInput = searchInput.where(_.not(_.isEmpty))
var isValidSearchInput = searchValidInput.selectAs(true)
.merge(searchInvalidInput.selectAs(false))
.startWith(false)
var searchKeypress = searchValidInput.distinctUntilChanged()
var searchButton = $('#search .controls button')
.onAsObservable('click')
.select(function() { return $('#search .controls input').val() })
.select($.trim)
.where(_.not(_.isEmpty))
var searchTerm = searchKeypress.merge(searchButton)
var searchResult = searchTerm
.select(searchServiceAsObservable)
.switchLatest()
.publish()
.refCount()
var isSearching = searchTerm.selectAs(true)
.merge(searchResult.selectAs(false))
.startWith(false)
searchResult
.where(isSuccessMaterial)
.selectProperty('value')
.selectProperty('0')
.subscribe(_.bind(Common.showSearchSuccess, null, $('#search .results')))
searchResult
.where(isFailureMaterial)
.selectProperty('exception')
.selectProperty('0')
.subscribe(_.bind(Common.showSearchFailure, null, $('#search .results')))
isSearching.subscribe(_.bind($.fn.toggleClass, $('#search .controls .searchButton'), 'loading'))
isSearching
.combineLatest(isValidSearchInput, function(searching, valid) { return searching || !valid })
.subscribe(_.bind($.fn.prop, $('#search .controls button'), 'disabled'))
}
function currentTargetValueOf(event) { return event.currentTarget.value }
function isSuccessMaterial(notification) { return notification.kind === 'N' }
function isFailureMaterial(notification) { return notification.kind === 'E' }
function searchServiceAsObservable(query) {
return $.Deferred.prototype.toObservable.apply(Common.searchService(query)).materialize()
}
})(window.jQuery, window._, window.Rx, window.App.Common, window.App)