(function ($) { STAG = { initSelect2RemoteSearch: function () { var select2SearchFields = $('[select2-search]'); console.log({select2SearchFields}); $.each(select2SearchFields, function (i, select) { select = $(select); var url = select.data('url'); var text = select.data('text-prop'); text = text.split('|'); var id = select.data('id-prop'); var uid = select.data('uid-prop'); var placeholder = select.attr('placeholder'); select.select2({ placeholder: placeholder, minimumInputLength: 2, ajax: { type: "GET", url: url, dataType: 'json', processResults: function (response) { var data = response.data; return { results: $.map(data, function (item) { var textString = ''; for (var i = 0; i < text.length; i++) { var prop = text[i]; var value = item[prop]; if (value) { textString = textString + value + ' '; } } return { text: textString, id: item[id] } }) }; } } }); }); }, init: function () { this.initSelect2RemoteSearch(); } }; STAG.init(); })(jQuery);