@@ -46,9 +46,9 @@ angular.module('ui.select', [])
46
46
47
47
/**
48
48
* Example:
49
- * expression = "address in getAddress( $select.search) track by $index
49
+ * expression = "address in addresses | filter: {street: $select.search} track by $index"
50
50
* lhs = "address",
51
- * rhs = "getAddress( $select.search) ",
51
+ * rhs = "addresses | filter: {street: $select.search} ",
52
52
* trackByExp = "$index",
53
53
* valueIdentifier = "address",
54
54
* keyIdentifier = undefined
@@ -97,8 +97,8 @@ angular.module('ui.select', [])
97
97
* put as much logic in the controller (instead of the link functions) as possible so it can be easily tested.
98
98
*/
99
99
. 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 ) {
102
102
103
103
var ctrl = this ;
104
104
@@ -128,44 +128,23 @@ angular.module('ui.select', [])
128
128
}
129
129
} ;
130
130
131
- var _repeatRhsIsCollection = null ;
132
- var _repeatRhsFn = null ;
133
-
134
131
ctrl . parseRepeatAttr = function ( repeatAttr ) {
135
132
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 ) ;
144
134
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 ) ;
150
138
}
151
- } ;
152
139
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
+ } ) ;
163
144
} ;
164
145
165
146
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 ) {
169
148
$timeout ( function ( ) {
170
149
$scope . $apply ( refreshAttr ) ;
171
150
} ) ;
@@ -182,11 +161,7 @@ angular.module('ui.select', [])
182
161
ctrl . close = function ( ) {
183
162
if ( ctrl . open ) {
184
163
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 ;
190
165
}
191
166
} ;
192
167
@@ -369,7 +344,6 @@ angular.module('ui.select', [])
369
344
scope . $watch ( '$select.search' , function ( ) {
370
345
$select . activeIndex = 0 ;
371
346
$select . refresh ( attrs . refresh ) ;
372
- $select . populateItems ( ) ;
373
347
} ) ;
374
348
} ;
375
349
}
0 commit comments