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

fix(autocomplete): only handle results if it's an array or a promise #7089

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,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 @@ -635,10 +635,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is PR is kinda old, but just to throw my 2 cents: Angular has an internal function, called isPromiseLike that checks whether an object is a promise like this: obj && isFunction(obj.then), maybe we could use the same?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devversion - Can you use isPromiseLike( ) also?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a public function but the check isn't that large.


if ( isList ) handleResults(items);
else handleAsyncResults(items);
if (isList) handleResults(items);
else if (isPromise) handleAsyncResults(items);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Messing scenario: what if not a list and not a Promise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about doing nothing, because it makes no sense, and otherwise it will prevent the autocomplete from working. See the demo of the actual issue. http://codepen.io/anon/pen/Gowjmv?editors=1010


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