I am using knockout js with select2. I am unable to get the custom matcher to enter the function i have created as i think it is not in the correct context.
HTML
knockout initaliser in the jquery initialize function
ko.bindingHandlers.scheme = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
//debugger
var options = ko.unwrap(valueAccessor());
ko.unwrap(allBindings.get(‘selectedOptions’));
$(element).prepend(’’);
$(element).select2(options);
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
//debugger
console.log(“scheme updated”);
var options = ko.unwrap(valueAccessor());
ko.unwrap(allBindings.get(‘selectedOptions’));
var el = $(element).select2(options);
var data = $(element).select2(‘data’);
var vm = bindingContext.$data;
if (data[0].id != "") {
}
}
};
I have tried a function in the jquery initializer
$(function () {
ko.bindingHandlers.select2 = {
matchCustom: function (params, data) {
debugger
}
};
});
I have also tried a function in a seperate js file in the same folder as the select2 js files
select2custom.js
function matchCustom(params, data) {
debugger
// If there are no search terms, return all of the data
if ($.trim(params.term) === ‘’) {
return data;
}
// Do not display the item if there is no 'text' property
if (typeof data.text === 'undefined') {
return null;
}
// `params.term` should be the term that is used for searching
// `data.text` is the text that is displayed for the data object
if (data.text.indexOf(params.term) > -1) {
var modifiedData = $.extend({}, data, true);
modifiedData.text += ' (matched)';
// You can return modified objects from here
// This includes matching the `children` how you want in nested data sets
return modifiedData;
}
// Return `null` if the term should not be displayed
return null;
}
When i put a breakpoint on the return statement below, it always errors with Uncaught TypeError: matcher is not a function
SelectAdapter.prototype.matches = function (params, data) {
var matcher = this.options.get(‘matcher’);
return matcher(params, data);
};