This repository was 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
Expand file tree
/
Copy pathapp.engine.bacon.js
More file actions
41 lines (30 loc) · 1.45 KB
/
Copy pathapp.engine.bacon.js
File metadata and controls
41 lines (30 loc) · 1.45 KB
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
(function($, _, Bacon, Common, exports) {
exports.startEngine = startEngine
function startEngine() {
var searchInput = $('#search .controls input')
.asEventStream('keyup')
.debounce(500)
.map('.currentTarget.value')
.map($.trim)
var searchInvalidInput = searchInput.filter(_.isEmpty)
var searchValidInput = searchInput.filter(_.not(_.isEmpty))
var isValidSearchInput = searchValidInput.awaiting(searchInvalidInput)
var searchKeypress = searchValidInput.skipDuplicates()
var searchButton = $('#search .controls button')
.asEventStream('click')
.map(function() { return $('#search .controls input').val() })
.map($.trim)
.filter(_.not(_.isEmpty))
var searchTerm = searchKeypress.merge(searchButton)
var searchResult = searchTerm
.map(Common.searchService)
.flatMapLatest(Bacon.fromPromise)
var isSearching = searchTerm.awaiting(searchResult).mapError(false)
searchResult.onValue(_.bind(Common.showSearchSuccess, null, $('#search .results')))
searchResult.onError(_.bind(Common.showSearchFailure, null, $('#search .results')))
isSearching.assign($('#search .controls .searchButton'), 'toggleClass', 'loading')
isSearching
.combine(isValidSearchInput, function(searching, valid) { return searching || !valid })
.assign($('#search .controls button'), 'prop', 'disabled')
}
})(window.jQuery, window._, window.Bacon, window.App.Common, window.App)