Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 55a3f7a

Browse files
committed
Watch repeat.rhs if it is a collection
1 parent ee00911 commit 55a3f7a

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

src/select.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ angular.module('ui.select', [])
9292

9393
var ctrl = this;
9494

95+
var EMPTY_SEARCH = '';
96+
9597
ctrl.placeholder = undefined;
96-
ctrl.search = undefined;
98+
ctrl.search = EMPTY_SEARCH;
9799
ctrl.activeIndex = 0;
98100
ctrl.items = [];
99101
ctrl.selected = undefined;
@@ -113,12 +115,38 @@ angular.module('ui.select', [])
113115
}
114116
};
115117

116-
ctrl.getItemsAsync = function(repeatAttr) {
118+
var _repeatRhsIsCollection = null;
119+
var _repeatRhsFn = null;
120+
121+
ctrl.parseRepeatAttr = function(repeatAttr) {
117122
var repeat = RepeatParser.parse(repeatAttr);
118-
var getter = $parse(repeat.rhs);
119-
$q.when(getter($scope)).then(function(items) {
120-
ctrl.items = items;
121-
});
123+
_repeatRhsFn = $parse(repeat.rhs);
124+
125+
var collectionOrPromise = _repeatRhsFn($scope);
126+
127+
// Hackish :/
128+
// Determine if the repeat expression (repeat.rhs) gives us a collection or a promise
129+
// If it is a collection we need to $watch it in order to update ctrl.items
130+
_repeatRhsIsCollection = angular.isArray(collectionOrPromise);
131+
132+
if (_repeatRhsIsCollection) {
133+
// See https://github.com/angular/angular.js/blob/55848a9139/src/ng/directive/ngRepeat.js#L259
134+
$scope.$watchCollection(repeat.rhs, function(items) {
135+
ctrl.items = items;
136+
});
137+
}
138+
};
139+
140+
ctrl.populateItems = function() {
141+
if (!_repeatRhsIsCollection) {
142+
var promise = _repeatRhsFn($scope);
143+
144+
// See https://github.com/angular-ui/bootstrap/blob/d0024931de/src/typeahead/typeahead.js#L109
145+
// See https://github.com/mgcrea/angular-strap/blob/1529ab4bbc/src/helpers/parse-options.js#L35
146+
$q.when(promise).then(function(items) {
147+
ctrl.items = items;
148+
});
149+
}
122150
};
123151

124152
// When the user clicks on an item inside the dropdown list
@@ -129,8 +157,14 @@ angular.module('ui.select', [])
129157
};
130158

131159
ctrl.close = function() {
132-
ctrl.open = false;
133-
ctrl.search = '';
160+
if (ctrl.open) {
161+
ctrl.open = false;
162+
if (_repeatRhsIsCollection) {
163+
// This means if repeat.rhs is a promise, we keep the search term (ctrl.search)
164+
// even after the dropdown being closed
165+
ctrl.search = EMPTY_SEARCH;
166+
}
167+
}
134168
};
135169

136170
var Key = {
@@ -289,9 +323,11 @@ angular.module('ui.select', [])
289323
.attr('ng-click', '$select.select(' + repeat.lhs + ')');
290324

291325
return function link(scope, element, attrs, $select) {
326+
$select.parseRepeatAttr(attrs.repeat);
327+
292328
scope.$watch('$select.search', function() {
293329
$select.activeIndex = 0;
294-
$select.getItemsAsync(attrs.repeat);
330+
$select.populateItems(attrs.repeat);
295331
});
296332
};
297333
}

0 commit comments

Comments
 (0)