Add decorator to data adapter in Select2 version 4.x

蹲街弑〆低调 提交于 2019-12-06 12:41:45

You'll notice in the gist that their call to decorate their adapter looks like

var dropdownAdapter = Utils.Decorate(
  Utils.Decorate(
    DropdownAdapter,
    DropdownSearch
  ),
  AttachContainer
);

And they later go on to use dropdownAdapter when initializing Select2. This is because Utils.Decorate returns the decorated class instead of decorating it in-place. You'll also notice that it does this after the decorator and adapters have already been created.

So instead of calling Utils.Decorate in the middle of your adapter, you should call it at the end. Either when you are returning the finished adapter, or right before you do.

define([...], function(Utils, MinimumInputLength, ArrayAdapter){    
    function MyDataAdapter($element, options) {
        this.$element = $element;
        this.options = options;
        MyDataAdapter.__super__.constructor.call(this, $element, options);
    }

    // Always extend after making the constructor
    Utils.Extend(MyDataAdapter, ArrayAdapter);

    MyDataAdapter.prototype.query = function(params, callback) {
        console.log('query!');
    };

    // Decorate after the adapter is built
    return Utils.Decorate(MyDataAdapter, MinimumInputLength);
});

Before your major issue was that you were not using the returned class from Utils.Decorate. But the other issue that you would have hit is that it just wouldn't work, which was because you were overriding the query method after it had been decorated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!