admin管理员组文章数量:1304017
I want to support the MaximumInputLength
decorator for my custom Select2 Data Adapter. In my custom data adapter, the Utils.Decorate()
call does not do anything.
Looking at this gist, I could call the Decorator function in the same place where I initialize select2()
, but that seems icky and not DRY, as I want to initialize many of these Select elements.
In order to enable minimum input for ALL instances of MyDataAdapter, is it possible to decorate that adapter from the adapter itself? Is there a better way to do this?
My select2()
call:
$('select').select2({
dataAdapter: MyDataAdapter,
minimumInputLength: 2
});
MyDataAdapter
(sans dependencies):
define([...], function(Utils, MinimumInputLength, ArrayAdapter){
function MyDataAdapter($element, options) {
this.$element = $element;
this.options = options;
MyDataAdapter.__super__.constructor.call(this, $element, options);
}
Utils.Extend(MyDataAdapter, ArrayAdapter);
Utils.Decorate(MyDataAdapter, MinimumInputLength); <-- does nothing
MyDataAdapter.prototype.query = function(params, callback) {
console.log('query!');
};
return MyDataAdapter;
});
I want to support the MaximumInputLength
decorator for my custom Select2 Data Adapter. In my custom data adapter, the Utils.Decorate()
call does not do anything.
Looking at this gist, I could call the Decorator function in the same place where I initialize select2()
, but that seems icky and not DRY, as I want to initialize many of these Select elements.
In order to enable minimum input for ALL instances of MyDataAdapter, is it possible to decorate that adapter from the adapter itself? Is there a better way to do this?
My select2()
call:
$('select').select2({
dataAdapter: MyDataAdapter,
minimumInputLength: 2
});
MyDataAdapter
(sans dependencies):
define([...], function(Utils, MinimumInputLength, ArrayAdapter){
function MyDataAdapter($element, options) {
this.$element = $element;
this.options = options;
MyDataAdapter.__super__.constructor.call(this, $element, options);
}
Utils.Extend(MyDataAdapter, ArrayAdapter);
Utils.Decorate(MyDataAdapter, MinimumInputLength); <-- does nothing
MyDataAdapter.prototype.query = function(params, callback) {
console.log('query!');
};
return MyDataAdapter;
});
Share
Improve this question
asked Jun 3, 2015 at 21:24
Benjamin SmithBenjamin Smith
8871 gold badge9 silver badges24 bronze badges
1 Answer
Reset to default 9You'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.
本文标签: javascriptAdd decorator to data adapter in Select2 version 4xStack Overflow
版权声明:本文标题:javascript - Add decorator to data adapter in Select2 version 4.x - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741778034a2397142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论