Adding options for multiple decorators
The current implementation only allows to overwrite the adapters.
If a user only wants to add a decorator they have to call Defaults.apply(options) prior to select2() to build the default adapters with its optional decorators based on the given options and they have to import the utils module to build it.
The problem is, that since you only had the option to change the whole adapter, all the plugins out there build their own adapters, without adding the default decorators. That way you cannot even combine two whole plugin adapters.
It's better to incentivize devs to actually use the decorator system!
With this new implementation you just have to pass an array of decorators, which are added in order to the default adapters (based on the current options)
This looks like a dead project, so here's a polyfill for those who want to use it while this isn't merged
$.fn.select2.amd.require(['select2/defaults', "select2/utils"],
function (Defaults, Utils) {
let __super__apply = Defaults.apply.bind(Defaults);
const adapterFields = Object.entries(__super__apply({})).reduce((pV,[k,v]) => {
if (v.prototype && v.prototype.bind) {
pV.push(k);
}
return pV;
},
[]);
Defaults.apply = function(options) {
const opt = __super__apply(options);
for(const adapter of adapterFields) {
const decorators = options[adapter + 'Decorators'];
if(decorators instanceof Array)
{
for(const decorator of decorators) {
opt[adapter] = Utils.Decorate(opt[adapter], decorator);
}
}
}
return opt;
}
}, null, true // forceSync = true
);
This pull request includes a
- [ ] Bug fix
- [X] New feature
- [ ] Translation
The following changes were made
- added following options to pass an array of decorators:
-
dataAdapterDecorators -
resultsAdapterDecorators -
dropdownAdapterDecorators -
selectionAdapterDecorators
-