Skip to content
This repository was archived by the owner on Jan 27, 2018. It is now read-only.

Commit 3f60c50

Browse files
committed
Defer resolving model against options on scope
Fixes issue with the model updating while the options are empty or missing, but the options update in the same digest cycle.
1 parent fca3ac2 commit 3f60c50

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

angular-selectize.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@
4141
var optionsProperty = match[7];
4242
var displayFn = $parse(match[2] || match[1]);
4343
var valueFn = $parse(match[2] ? match[1] : valueName);
44-
var selectize, newSelections, newOptions, updateTimer;
44+
var selectize, newModelValue, newOptions, updateTimer;
4545

4646
scope.$watchCollection(function() {
4747
return ngModelCtrl.$modelValue;
4848
}, function(modelValue) {
4949
if (!selectize) {
5050
return;
5151
}
52-
if (!newSelections) {
53-
newSelections = getSelectedItems(modelValue);
52+
if (!newModelValue) {
53+
newModelValue = modelValue;
5454
}
5555
if (!updateTimer) {
5656
scheduleUpdate();
@@ -69,7 +69,7 @@
6969

7070
function scheduleUpdate() {
7171
updateTimer = $timeout(function() {
72-
var selected = newSelections || selectize.items;
72+
var selected = newModelValue ? getSelectedItems(newModelValue) : selectize.items;
7373
selectize.clear();
7474
if (newOptions) {
7575
selectize.clearOptions();
@@ -85,7 +85,7 @@
8585
selected.forEach(function(item) {
8686
selectize.addItem(item);
8787
});
88-
newSelections = null;
88+
newModelValue = null;
8989
newOptions = null;
9090
updateTimer = null;
9191
});

test/select.spec.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('<select selectize>', function() {
4040

4141
function testSelectedOption(value) {
4242
var domOptions = selectElement.find('option');
43-
var selectedOption = scope.options[parseInt(domOptions.attr('value'), 10)];
43+
var selectedOption = scope.options[parseInt(domOptions.attr('value'), 10)] || '';
4444
assert.strictEqual(domOptions.length, 1);
4545
assert.ok(domOptions.attr('selected'));
4646
assert.equal(selectedOption.value || selectedOption, value);
@@ -107,26 +107,50 @@ describe('<select selectize>', function() {
107107
});
108108

109109
describe('when both the model and the options are updated', function() {
110-
it('should have the same number of options in the dropdown menu as scope.options', function() {
111-
assert.strictEqual(selectize.$dropdown_content.children().length, scope.options.length);
110+
describe('when both the model and options start empty', function() {
111+
beforeEach(function() {
112+
scope.selection = undefined;
113+
scope.options = [];
114+
scope.$apply();
115+
timeout.flush();
116+
});
112117

113-
scope.selection = 'bar';
114-
scope.options.push('qux');
115-
scope.$apply();
116-
timeout.flush();
118+
it('should update the selection', function() {
119+
testSelectedOption('');
120+
121+
scope.selection = 'foo';
122+
scope.$apply();
123+
scope.options = ['foo', 'bar', 'baz'];
124+
scope.$apply();
125+
timeout.flush();
126+
127+
testSelectedOption('foo');
128+
});
117129

118-
assert.strictEqual(selectize.$dropdown_content.children().length, scope.options.length);
119130
});
120131

121-
it('should update the selection', function() {
122-
testSelectedOption('foo');
132+
describe('when both the model and options start non-empty', function() {
133+
it('should have the same number of options in the dropdown menu as scope.options', function() {
134+
assert.strictEqual(selectize.$dropdown_content.children().length, scope.options.length);
123135

124-
scope.selection = 'bar';
125-
scope.options.push('qux');
126-
scope.$apply();
127-
timeout.flush();
136+
scope.selection = 'bar';
137+
scope.options.push('qux');
138+
scope.$apply();
139+
timeout.flush();
128140

129-
testSelectedOption('bar');
141+
assert.strictEqual(selectize.$dropdown_content.children().length, scope.options.length);
142+
});
143+
144+
it('should update the selection', function() {
145+
testSelectedOption('foo');
146+
147+
scope.selection = 'bar';
148+
scope.options.push('qux');
149+
scope.$apply();
150+
timeout.flush();
151+
152+
testSelectedOption('bar');
153+
});
130154
});
131155
});
132156
});

0 commit comments

Comments
 (0)