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

Commit f46e92c

Browse files
committed
Repeat expression cannot be a promise after all
Replaced by refresh attribute => more flexible
1 parent 9920e5d commit f46e92c

File tree

1 file changed

+14
-40
lines changed

1 file changed

+14
-40
lines changed

src/select.js

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ angular.module('ui.select', [])
4646

4747
/**
4848
* Example:
49-
* expression = "address in getAddress($select.search) track by $index
49+
* expression = "address in addresses | filter: {street: $select.search} track by $index"
5050
* lhs = "address",
51-
* rhs = "getAddress($select.search)",
51+
* rhs = "addresses | filter: {street: $select.search}",
5252
* trackByExp = "$index",
5353
* valueIdentifier = "address",
5454
* keyIdentifier = undefined
@@ -97,8 +97,8 @@ angular.module('ui.select', [])
9797
* put as much logic in the controller (instead of the link functions) as possible so it can be easily tested.
9898
*/
9999
.controller('uiSelectCtrl',
100-
['$scope', '$element', '$timeout', 'RepeatParser', '$parse', '$q', 'uiSelectMinErr',
101-
function($scope, $element, $timeout, RepeatParser, $parse, $q, uiSelectMinErr) {
100+
['$scope', '$element', '$timeout', 'RepeatParser', '$parse', 'uiSelectMinErr',
101+
function($scope, $element, $timeout, RepeatParser, $parse, uiSelectMinErr) {
102102

103103
var ctrl = this;
104104

@@ -128,44 +128,23 @@ angular.module('ui.select', [])
128128
}
129129
};
130130

131-
var _repeatRhsIsCollection = null;
132-
var _repeatRhsFn = null;
133-
134131
ctrl.parseRepeatAttr = function(repeatAttr) {
135132
var repeat = RepeatParser.parse(repeatAttr);
136-
_repeatRhsFn = $parse(repeat.rhs);
137-
138-
var collectionOrPromise = _repeatRhsFn($scope);
139-
140-
// Hackish :/
141-
// Determine if the repeat expression (repeat.rhs) gives us a collection or a promise
142-
// If it is a collection we need to $watch it in order to update ctrl.items
143-
_repeatRhsIsCollection = angular.isArray(collectionOrPromise);
133+
var repeatRhsFn = $parse(repeat.rhs);
144134

145-
if (_repeatRhsIsCollection) {
146-
// See https://github.com/angular/angular.js/blob/55848a9139/src/ng/directive/ngRepeat.js#L259
147-
$scope.$watchCollection(repeat.rhs, function(items) {
148-
ctrl.items = items;
149-
});
135+
var collection = repeatRhsFn($scope);
136+
if (!angular.isArray(collection)) {
137+
throw uiSelectMinErr('repeat', "Expected a collection but got '{0}'.", repeat.rhs);
150138
}
151-
};
152139

153-
ctrl.populateItems = function() {
154-
if (!_repeatRhsIsCollection) {
155-
var promise = _repeatRhsFn($scope);
156-
157-
// See https://github.com/angular-ui/bootstrap/blob/d0024931de/src/typeahead/typeahead.js#L109
158-
// See https://github.com/mgcrea/angular-strap/blob/1529ab4bbc/src/helpers/parse-options.js#L35
159-
$q.when(promise).then(function(items) {
160-
ctrl.items = items;
161-
});
162-
}
140+
// See https://github.com/angular/angular.js/blob/55848a9139/src/ng/directive/ngRepeat.js#L259
141+
$scope.$watchCollection(repeat.rhs, function(items) {
142+
ctrl.items = items;
143+
});
163144
};
164145

165146
ctrl.refresh = function(refreshAttr) {
166-
// Only works if the repeat expression (repeat.rhs) is a collection,
167-
// does not make sense with a promise
168-
if (refreshAttr !== undefined && _repeatRhsIsCollection) {
147+
if (refreshAttr !== undefined) {
169148
$timeout(function() {
170149
$scope.$apply(refreshAttr);
171150
});
@@ -182,11 +161,7 @@ angular.module('ui.select', [])
182161
ctrl.close = function() {
183162
if (ctrl.open) {
184163
ctrl.open = false;
185-
if (_repeatRhsIsCollection) {
186-
// This means if repeat.rhs is a promise, we keep the search term (ctrl.search)
187-
// even after the dropdown being closed
188-
ctrl.search = EMPTY_SEARCH;
189-
}
164+
ctrl.search = EMPTY_SEARCH;
190165
}
191166
};
192167

@@ -369,7 +344,6 @@ angular.module('ui.select', [])
369344
scope.$watch('$select.search', function() {
370345
$select.activeIndex = 0;
371346
$select.refresh(attrs.refresh);
372-
$select.populateItems();
373347
});
374348
};
375349
}

0 commit comments

Comments
 (0)