I am trying to get Angular UI bootstraps typeahead working with a REST resource I have set up. But I am not sure how to get it working with it's asynchronous nature.
At the moment I have adapted the example given by Angular UI Bootstrap.
so my html looks like so, calling getLibs() to get the list for the typeahead dropdown.
<div class='container-fluid' ng-controller="TypeaheadCtrl">
<pre>Model: {{selected| json}}</pre>
<input type="text" typeahead="lib.name for lib in getLibs($viewValue)" active='active' ng-model="selected" typeahead-min-length='3' >
</div>
my resource looks like so:
angular.module('LibraryLookupService', ['ngResource']).factory('Library',
function($resource){
return $resource( "../../api/seq/library/?name__icontains=:searchStr" , {} ,
{ 'get': {method:'GET'},
'query': {method:'GET', params: {} , isArray:false },
}
)
}
);
my controller looks like so (I am guessing it is here I am doing something incorrect):
function TypeaheadCtrl($scope , Library){
$scope.selected = undefined;
$scope.active = undefined ;
$scope.libs = [{name:'initial'} , {name:"values"}];
$scope.getLibs = function(viewValue){
console.log("value:", viewValue);
if (viewValue.length > 3 ) {
var return_vals = Library.query({searchStr:viewValue}, function() {
$scope.libs = return_vals.objects ;
console.log("here", $scope.libs) ;
}) ;
}
return $scope.libs
}
}
So as I understand it, the typeahead menu is being populated from the return value of the getLibs() function. When getLibs() is called it is querying the REST interface, but initially an empty value is returned. This is being populated by the function supplied to the Library.query() method, and this is done after the data is returned from the REST request.
This means basically that the menu is being updated one keypress later than what I want. I type '3456' and it gets populated with results of a '345' query to the REST interface.
How do I get the menu to update when the response is returned from the Library.query() function? Am I going about this correctly?