1

I am trying to implement autoSearch in angular (+jqeuryEasyUI). I use mongoDB as the database.

In my controller, I wrote something like that:

$scope.$watch('searchInput.text', function() {
  $timeout(functoin() {
    $scope.performSearch($scope.searchInput.text);
    }, 1500)
}

The function is called every time the user types another char in the search box input, and the function: performSearch(searchInput) is send a GET request to the mongoDB to retrieve data for the current input string.

My problem is that if the user type,say, 3 letters , I want to cancel the request for the first letter and the request for the 2 first letter, and search only the 3 letter together.

That way I used the Timeout function, that any change the accuer in this time will cancel the previous request.

How can I mange the request? How do I tell the $watch function that if there was any change, don't send the request?

I heard of working with promises but I can't figure out how can it help in this situation.

Thank you very much!

3 Answers 3

1

Have you considered using AngulaUI-Bootstrap typeahead feature? Refer to this link. It has configuration for timeout, minimum characters for firing search and others. It's east to implement as well.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you! in the end, you were right.. angular-bootstrap has functoin that can handel that..
1

Try this in your controller:

var timeoutHandle;

$scope.$watch('search', function() {
  $timeout.cancel(timeoutHandle);
  timeoutHandle = $timeout(function() {
    console.log('searching');
  }, 1500)
})

This will cancel the previous timeout and set a new one with each change of the input, so that a request will only be sent 1.5 seconds after the last change.

PLUNKER

Comments

0

I have this kind of functionality, heres sample code:

in controller

MyService.delayCall(function() {

            MyService.someFunc(function(data) {
                ...
            });

        },400);

in services

 delayCallId : 0,
       delayCall : function(call,milisecs) {
           this.delayCallId++;
           var xxx = this;
           var myId = this.delayCallId;
           $timeout(function() {
               if (xxx.delayCallId === myId) {
                   call();
               }
           }, milisecs);
       }

someFuncRequest: 1,
        someFunc : function(callback) {
            this.doyouwantRequest++;
            var xxx = this;
            $http.get(
                    '/some/rest/function?requestId='
                    + this.someFuncRequest).success(
                    function(data, status) {
                        if (xxx.someFuncRequest == data.id) {
                            callback(data);
                        }
                    }).error(function(data, status) {
                callback(data);
            });
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.