Skip to content

Commit 8c6fa7a

Browse files
author
Marc J. Schmidt
committed
Merge pull request marcj#119 from pavelhoral/master
`setOptions` now depends on `refresh` and not vice-versa
2 parents cc3df32 + 89db69d commit 8c6fa7a

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
$('#console')[0].scrollTop = $('#console')[0].scrollHeight;
5151
});
5252

53+
$('#refresh').click(function() {
54+
$('SELECT OPTION').each(function() {
55+
$(this).text('Refreshed ' + $(this).val());
56+
});
57+
$('SELECT').selectBox('refresh');
58+
});
59+
5360
$('#value-1').click(function () {
5461
$('SELECT').selectBox('value', 1);
5562
});
@@ -337,6 +344,7 @@ <h1>$('select').selectBox();</h1>
337344
<input type="button" id="enable" value="Enable"/>
338345
<input type="button" id="disable" value="Disable"/>
339346
<input type="button" id="serialize" value="Serialize Form"/>
347+
<input type="button" id="refresh" value="Refresh" />
340348
</p>
341349

342350
<p>

jquery.selectBox.js

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,31 @@
401401
* Refreshes the option elements.
402402
*/
403403
SelectBox.prototype.refresh = function () {
404-
var select = $(this.selectElement),
405-
control = select.data('selectBox-control'),
406-
dropdown = control.hasClass('selectBox-dropdown'),
407-
menuOpened = control.hasClass('selectBox-menuShowing');
408-
select.selectBox('options', select.html());
404+
var select = $(this.selectElement)
405+
, control = select.data('selectBox-control')
406+
, type = control.hasClass('selectBox-dropdown') ? 'dropdown' : 'inline'
407+
, options;
408+
409+
// Remove old options
410+
control.data('selectBox-options').remove();
411+
412+
// Generate new options
413+
options = this.getOptions(type);
414+
control.data('selectBox-options', options);
415+
416+
switch (type) {
417+
case 'inline':
418+
control.append(options);
419+
break;
420+
case 'dropdown':
421+
// Update label
422+
this.setLabel();
423+
$("BODY").append(options);
424+
break;
425+
}
426+
409427
// Restore opened dropdown state (original menu was trashed)
410-
if (dropdown && menuOpened) {
428+
if ('dropdown' === type && control.hasClass('selectBox-menuShowing')) {
411429
this.showMenu();
412430
}
413431
};
@@ -922,9 +940,7 @@
922940
*/
923941
SelectBox.prototype.setOptions = function (options) {
924942
var select = $(this.selectElement)
925-
, control = select.data('selectBox-control')
926-
, settings = select.data('selectBox-settings')
927-
, type;
943+
, control = select.data('selectBox-control');
928944

929945
switch (typeof(options)) {
930946
case 'string':
@@ -950,27 +966,9 @@
950966
break;
951967
}
952968

953-
if (!control) {
954-
return;
955-
}
956-
957-
// Remove old options
958-
control.data('selectBox-options').remove();
959-
960-
// Generate new options
961-
type = control.hasClass('selectBox-dropdown') ? 'dropdown' : 'inline';
962-
options = this.getOptions(type);
963-
control.data('selectBox-options', options);
964-
965-
switch (type) {
966-
case 'inline':
967-
control.append(options);
968-
break;
969-
case 'dropdown':
970-
// Update label
971-
this.setLabel();
972-
$("BODY").append(options);
973-
break;
969+
if (control) {
970+
// Refresh the control
971+
this.refresh();
974972
}
975973
};
976974

0 commit comments

Comments
 (0)