Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding options for multiple decorators #6309

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

5andr0
Copy link

@5andr0 5andr0 commented May 1, 2024

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.name === 'DecoratedClass' || v.__super__) {
        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
  • New feature
  • Translation

The following changes were made

  • added following options to pass an array of decorators:
    • dataAdapterDecorators
    • resultsAdapterDecorators
    • dropdownAdapterDecorators
    • selectionAdapterDecorators

The current implementation only allows to overwrite the adapters. If a user only wants to add a decorator they have to call defaults.apply to build the default adapter based on the given options for different decorators and they have to import the utils module to build it. 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 passed options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant