How to change jquery select2 method ($ .fn.select2) using underscore _.wrap method

I have below HTML code for select2 combobox ( pnlkr link )

<label id="label-select2Home">My Home State: </label>
<div class="select2-home" xe-field="select2Home"></div>

JS to initialize:

$( ".select2-home" ).select2({
        tokenSeparators: [",", " "],
        allowClear: true,
        placeholder: "Select a state",
        data: stateData
});

WHAT TO DO

I need to find a shortcut nearby and change the text to "TEST LABEL".

I am trying to do this with

$.fn.select2 = _.wrap($.fn.select2, function changeLabel(org) {
  // Replace the nearby label text with "TEST LABEL"
});

I can not do with

 // Replace the nearby label text with "TEST LABEL"
var args = Array.prototype.slice.call(arguments, 1);  // use Array.slice function to copy the arguments array from 1-end, without 'org'
        return org.apply( this, args ); 

This gives me an error. How to call .apply method to control select2 and change label text.

See plnkr for the above example

+4
source share
1 answer

, _.wrap , $.fn.select2. $.fn.select2.defaults, .

, - :

$.fn.select2 = _.wrap($.fn.select2, function changeLabel(org) {
    // Replace the nearby label text with "TEST LABEL"

    $.fn.select2.defaults = org.defaults;
    var args = Array.prototype.slice.call(arguments, 1);
    return org.apply(this, args);
});
+2

All Articles