Reusing select2.js in Plone 5

Plone 5 ships with select2.js as part of mockup

I also have the requirement to use select2.js in my own add-on, in particular for "improving" some SELECT
elements of a Dexterity content-type within the edit view. So I created a small JS registered with my add-on in order to trigger the select2 functionality upon loading

$(document).ready(function() {
    $('some.selector select').select2();
})

However this leads to the typical

$(...).select2 is not a function 

error.

So how to re-use existing JS libs shipped with Plone out-of-the-box in my own add-ons?

-aj

pretty sure you can use the select2 pattern by using a directives.widget to insert class="pat-select2" on your select input. Patterns aren't meant to be used from javascript but from html. You can use it as above only if you use requirejs to import select2 code. Thats my understanding anyway.

or just use the provided select2 widget? https://pypi.python.org/pypi/plone.app.widgets/1.3.2

here is a snippet of my code where I'm using select2

require([
    'jquery',
    'pat-base',
    'mockup-patterns-modal',
    'mockup-patterns-select2',
    'mockup-patterns-sortable',
    "pat-registry"
], function($, Base, Modal, Select2, sortable, registry) {