-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiselect.js
51 lines (47 loc) · 1.66 KB
/
Multiselect.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
Template.Multiselect.onRendered(function multiselectOnRendered() {
let template = this;
let config = {};
if(template.data.configOptions) {
config = template.data.configOptions;
}
// autorun waits until after the dependent data has been updated
template.autorun(function multiselectAutorun() {
Template.currentData();
// afterFlush waits until the rest of the DOM elements have been built
Tracker.afterFlush(function multiselectAfterFlush() {
if(template.data.menuItems.length > 0) {
// Finally ready to initialize the multiselect
// e.g. after #each has completed creating all elements
// If data has updated refresh the multiselect
if(template.selector) {
template.selector.multiselect('refresh');
} else {
template.selector = template.selector || template.$('select');
template.selector.multiselect(config);
}
}
});
});
});
Template.Multiselect.helpers({
'args': function args() {
let template = Template.instance();
let data = template.data;
let selected = false;
if(data.selectedList instanceof Array) {
selected = Boolean(data.selectedList.indexOf(this.value) > -1 );
} else {
selected = this.value === data.selectedList;
}
// if multiselect is initiliazed then any data changes will
// need to update the multiselect internal data
if(template.selector) {
if(selected) {
template.selector.multiselect('select', this.value);
} else {
template.selector.multiselect('deselect', this.value);
}
}
return _.extend({}, this, {'selectedAttr': selected ? 'selected' : ''});
}
});