你会注意到the gist https://gist.github.com/timkelty/ae79d180acc9d3054e02他们装饰适配器的调用看起来像
var dropdownAdapter = Utils.Decorate(
Utils.Decorate(
DropdownAdapter,
DropdownSearch
),
AttachContainer
);
他们后来继续使用dropdownAdapter
初始化 Select2 时。这是因为Utils.Decorate
returns装饰类而不是就地装饰它。你还会注意到它是这样做的after装饰器和适配器已经创建。
所以而不是打电话Utils.Decorate
在适配器的中间,您应该在最后调用它。无论是在您归还完成的适配器时,还是在您归还之前。
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);
});
在您的主要问题是您没有使用从返回的类之前Utils.Decorate
。但是您会遇到的另一个问题是它不起作用,这是因为您覆盖了query
装饰后的方法。