Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(autocomplete): only handle results if it's an array or a promise
Browse files Browse the repository at this point in the history
At the moment the autocomplete will handle the results wrong.
So if we specify for example an empty JSON-Object, the autocomplete will handle the results as an async promise.

Fixes #7074

Closes #7089
  • Loading branch information
devversion authored and ThomasBurleson committed Mar 30, 2016
1 parent bbbe9e3 commit 05a08c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,36 @@ describe('<md-autocomplete>', function() {
element.remove();
}));

it('should not show a loading progress when the items object is invalid', inject(function() {
var scope = createScope(null, {
match: function() {
// Return an invalid object, which is not an array, neither a promise.
return {}
}
});

var template =
'<md-autocomplete ' +
'md-input-id="{{inputId}}" ' +
'md-selected-item="selectedItem" ' +
'md-search-text="searchText" ' +
'md-items="item in match(searchText)" ' +
'md-item-text="item.display" ' +
'tabindex="3"' +
'placeholder="placeholder">' +
'<span md-highlight-text="searchText">{{item.display}}</span>' +
'</md-autocomplete>';

var element = compile(template, scope);
var ctrl = element.controller('mdAutocomplete');

scope.$apply('searchText = "test"');

expect(ctrl.loading).toBe(false);

element.remove();
}));

it('should clear value when hitting escape', inject(function($mdConstant, $timeout) {
var scope = createScope();
var template = '\
Expand Down
7 changes: 4 additions & 3 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,11 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
function fetchResults (searchText) {
var items = $scope.$parent.$eval(itemExpr),
term = searchText.toLowerCase(),
isList = angular.isArray(items);
isList = angular.isArray(items),
isPromise = !!items.then; // Every promise should contain a `then` property

if ( isList ) handleResults(items);
else handleAsyncResults(items);
if (isList) handleResults(items);
else if (isPromise) handleAsyncResults(items);

function handleAsyncResults(items) {
if ( !items ) return;
Expand Down

0 comments on commit 05a08c8

Please sign in to comment.