-
Notifications
You must be signed in to change notification settings - Fork 64
/
TypeaheadCompositeSource.js
77 lines (66 loc) · 1.87 KB
/
TypeaheadCompositeSource.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
69
70
71
72
73
74
75
76
77
/**
* @requires javelin-install
* javelin-typeahead-source
* javelin-util
* @provides javelin-typeahead-composite-source
* @javelin
*/
/**
* @group control
*/
JX.install('TypeaheadCompositeSource', {
extend : 'TypeaheadSource',
construct : function(sources) {
JX.TypeaheadSource.call(this);
this.sources = sources;
for (var ii = 0; ii < this.sources.length; ++ii) {
var child = this.sources[ii];
child.listen('waiting', JX.bind(this, this.childWaiting));
child.listen('resultsready', JX.bind(this, this.childResultsReady));
child.listen('complete', JX.bind(this, this.childComplete));
}
},
members : {
sources : null,
results : null,
completeCount : 0,
didChange : function(value) {
this.results = [];
this.completeCount = 0;
for (var ii = 0; ii < this.sources.length; ++ii) {
this.sources[ii].didChange(value);
}
},
didStart : function() {
for (var ii = 0; ii < this.sources.length; ++ii) {
this.sources[ii].didStart();
}
},
childWaiting : function() {
if (!this.results || !this.results.length) {
this.invoke('waiting');
}
},
childResultsReady : function(nodes) {
this.results = this.mergeResults(this.results || [], nodes);
this.invoke('resultsready', this.results);
},
childComplete : function() {
this.completeCount++;
if (this.completeCount == this.sources.length) {
this.invoke('complete');
}
},
/**
* Overrideable strategy for combining results.
* By default, appends results as they come in
* so that results don't jump around.
*/
mergeResults : function(oldResults, newResults) {
for (var ii = 0; ii < newResults.length; ++ii) {
oldResults.push(newResults[ii]);
}
return oldResults;
}
}
});