-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathinit.js
More file actions
54 lines (43 loc) · 1.43 KB
/
init.js
File metadata and controls
54 lines (43 loc) · 1.43 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
42
43
44
45
46
47
48
49
50
51
52
53
54
(function() {
function initSearchableSelects(inputs, extra) {
inputs.each(function() {
var item = $(this);
// reading from data allows <input data-searchable_select='{"tags": ['some']}'>
// to be passed to select2
var options = $.extend(extra || {}, item.data('searchableSelect'));
// default option allow clear (must have placeholder and allowClear to options)
if (options.placeholder == undefined)
options.placeholder = '';
if (options.allowClear == undefined)
options.allowClear = true;
var url = item.data('ajaxUrl');
if (url) {
$.extend(options, {
ajax: {
url: url,
dataType: 'json',
data: function (params) {
return {
term: params.term,
page: pageParamWithBaseZero(params)
};
}
}
});
}
item.select2(options);
});
}
function pageParamWithBaseZero(params) {
return params.page ? params.page - 1 : undefined;
}
$(document).on('has_many_add:after', '.has_many_container', function(e, fieldset) {
initSearchableSelects(fieldset.find('.searchable-select-input'));
});
$(document).on('page:load turbolinks:load', function() {
initSearchableSelects($(".searchable-select-input"), {placeholder: ""});
});
$(function() {
initSearchableSelects($(".searchable-select-input"));
});
}());