最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select2 custom dataAdapter without AMD - Stack Overflow

programmeradmin2浏览0评论

I want to create a custom dataAdapter for select2 but the examples I see online all make use of AMD. We don't use AMD in our projects. How can I create my custom dataAdapter? A plain object that implements current and query methods is not enough.

I want to create a custom dataAdapter for select2 but the examples I see online all make use of AMD. We don't use AMD in our projects. How can I create my custom dataAdapter? A plain object that implements current and query methods is not enough.

Share Improve this question edited Oct 1, 2015 at 10:18 Thanasis Ioannidis asked Oct 1, 2015 at 9:53 Thanasis IoannidisThanasis Ioannidis 3,2311 gold badge36 silver badges53 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

It turns out you can almost pletely avoid the use of AMD. The following works for me with select2 version 4.0.10:

const ArrayAdapter = $.fn.select2.amd.require("select2/data/array");

class DataAdapter extends ArrayAdapter
{
    constructor($element, options)
    {
        super($element, options);
    }

    query(params, callback)
    {
        console.log("params: " + JSON.stringify(params));
    }
}

$("#my-bo-box").select2(
    {
        dataAdapter: DataAdapter
    }
);

You can ever go as far as accessing $.fn.select2.amd.require._defined["select2/data/array"] instead of invoking amd.require() but there is probably no point going this far :)

Select2 has a built-in AMD loader that it uses for loading plugins and adapters, so you would need to use that to build out your custom data adapter.

You can find examples of customized data adapters at Add decorator to data adapter in Select2 version 4.x

Instead of calling define directly, you would need to use the method provided by Select2 in jQuery.fn.select2.amd. So something like

define('something/awesome', ['select2/data/array', function (ArrayAdapter) {
  // Use the array adapter
}]);

would bee

jQuery.fn.select2.amd.define('something/awesome', ['select2/data/array', function (ArrayAdapter) {
  // Use the array adapter
}]);
发布评论

评论列表(0)

  1. 暂无评论