在Select2版本4.x中为数据适配器添加装饰器
Add decorator to data adapter in Select2 version 4.x
我想为我的自定义Select2数据适配器支持MaximumInputLength
装饰器。在我的自定义数据适配器中,Utils.Decorate()
调用不做任何事情。
看这个要点,我可以在我初始化select2()
的相同位置调用Decorator函数,但这似乎很讨厌,而不是DRY,因为我想初始化许多这些Select元素。
为了使MyDataAdapter的所有实例的输入最小,是否有可能从适配器本身修饰该适配器?有更好的方法吗?
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;
});
您将注意到,在要点中,他们对装饰适配器的调用看起来像
var dropdownAdapter = Utils.Decorate(
Utils.Decorate(
DropdownAdapter,
DropdownSearch
),
AttachContainer
);
然后在初始化Select2时继续使用dropdownAdapter
。这是因为Utils.Decorate
返回被装饰的类,而不是就地装饰它。您还会注意到,它在(已经创建了装饰器和适配器)之后执行。
所以不要在适配器中间调用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
方法后,它已经被装饰。
相关文章:
- RabbitMQ与通过网络的stomp适配器
- EmberJS-适用于各种模型的适配器动态名称空间
- Ember、JSONAPI适配器、JSONAPISerializer、findAll不可用
- 解析服务器中的mailgun模块(不是适配器)
- 如何通过ibmworklight中的sql适配器从db2中检索或显示html页面上的数据?android混合应用程序开发
- 本地存储适配器简介
- 如何在工作灯适配器中调用 WSDL
- ckeditor jquery适配器设置模板路径
- Worklight Javascript适配器:无法检索作为参数传递给适配器的数组值
- 在工作灯中的Http适配器中检索Lat和Long
- AngularJS+jQuery Mobile w/无适配器&禁用路由-仅用于UI样式
- Ember模型create()没有创建ID,适配器必须实现“;createRecord”;
- 在MobileFirst javascript适配器中使用xml输入调用DB存储过程
- 在AngularJS中使用jQuery移动适配器-应用jqmCompatMode false启用角度路由
- 错误:'历史记录.适配器'为null或不是对象
- 如何在Ember.js中禁用数据适配器以仅使用浏览器存储
- 如何在ember数据中添加适配器测试
- 在JS HTTP适配器中为MobileFirst 8添加方法
- 在 JS HTTP 适配器中为 MobileFirst 8 添加参数
- 在Select2版本4.x中为数据适配器添加装饰器