fix double initSelection call in select2

This commit is contained in:
vitalets 2013-09-21 21:21:48 +04:00
parent cc72d35664
commit 3166073666
2 changed files with 16 additions and 7 deletions
CHANGELOG.txt
src/inputs/select2

@ -3,6 +3,7 @@ X-editable changelog
Version 1.5.0 wip Version 1.5.0 wip
---------------------------- ----------------------------
[enh] update select2 to 3.4.3 (vitalets)
[enh #343] Bootstrap 3 support (vitalets) [enh #343] Bootstrap 3 support (vitalets)

@ -1,7 +1,7 @@
/** /**
Select2 input. Based on amazing work of Igor Vaynberg https://github.com/ivaynberg/select2. Select2 input. Based on amazing work of Igor Vaynberg https://github.com/ivaynberg/select2.
Please see [original select2 docs](http://ivaynberg.github.com/select2) for detailed description and options. Please see [original select2 docs](http://ivaynberg.github.com/select2) for detailed description and options.
Compatible **select2 version is 3.4.1**!
You should manually download and include select2 distributive: You should manually download and include select2 distributive:
<link href="select2/select2.css" rel="stylesheet" type="text/css"></link> <link href="select2/select2.css" rel="stylesheet" type="text/css"></link>
@ -142,9 +142,11 @@ $(function(){
$.extend(Constructor.prototype, { $.extend(Constructor.prototype, {
render: function() { render: function() {
this.setClass(); this.setClass();
//apply select2 //can not apply select2 here as it calls initSelection
this.$input.select2(this.options.select2); //over input that does not have correct value yet.
//apply select2 only in value2input
//this.$input.select2(this.options.select2);
//when data is loaded via ajax, we need to know when it's done to populate listData //when data is loaded via ajax, we need to know when it's done to populate listData
if(this.isRemote) { if(this.isRemote) {
@ -207,8 +209,14 @@ $(function(){
} }
*/ */
//for remote source just set value, text is updated by initSelection //for remote source just set value, text is updated by initSelection
this.$input.val(value).trigger('change', true); //second argument needed to separate initial change from user's click (for autosubmit) if(!this.$input.data('select2')) {
this.$input.val(value);
this.$input.select2(this.options.select2);
} else {
//second argument needed to separate initial change from user's click (for autosubmit)
this.$input.val(value).trigger('change', true);
}
//if remote source AND no user's initSelection provided --> try to use element's text //if remote source AND no user's initSelection provided --> try to use element's text
if(this.isRemote && !this.isMultiple && !this.options.select2.initSelection) { if(this.isRemote && !this.isMultiple && !this.options.select2.initSelection) {
@ -216,7 +224,7 @@ $(function(){
customText = this.options.select2.formatSelection; customText = this.options.select2.formatSelection;
if(!customId && !customText) { if(!customId && !customText) {
var data = {id: value, text: $(this.options.scope).text()}; var data = {id: value, text: $(this.options.scope).text()};
this.$input.select2('data', data); this.$input.select2('data', data);
} }
} }
}, },