1

I've got a call using Resource in angularjs but i get some problems because i can't abort every calls it does. This kind of structure i use for an autocomplete.. is it possible convert from resource call to http? This is the code

var Resource = $resource(URL, {},{ getAutocompleteResults: { method: "GET", params: {text: ""} }});

var locked = false;
function getMoreData() {

    if(locked)
        return;
    locked = true;

    Resource.autoCompleteResults()
        .$promise.then(function(data) {
            $scope.autocompleteViewResults = data;
            locked = false;
        });

}

This is what i've tried so far with no success.

$scope.autocompleteViewResults = function () {
            $http
                .get(URL, {
                    params: {
                        text = ""
                    }
                })
                .success(function (data) {
                    $scope.autocompleteViewResults = data;
                });
        };

Or if someone knows an alternative method..

1 Answer 1

2

The $scope.autocompleteViewResults variable is being assigned 2 times.

Try this:

$scope.autocompleteViewResults = {};
$scope.getResults = function(valueAsTyped) {
        $http
            .get(URL, {
                params: {
                    text: valueAsTyped
                }
            })
            .success(function (data) {
                $scope.autocompleteViewResults = data;
            });
  };

Update

If you need to cancel old requests.

var promiseCanceller = $q.defer();
$scope.autocompleteViewResults = {};

$scope.getResults = function(valueAsTyped) {

        promiseCanceller.resolve('request cancelled'); // cancel currently running request
        $http
            .get(URL, {
                params: {
                    text: valueAsTyped
                },
                timeout: promiseCanceller.promise // pass promiseCanceller as the timeout
            })
            .success(function (data) {
                $scope.autocompleteViewResults = data;
            });
  };
Sign up to request clarification or add additional context in comments.

7 Comments

tried..but i get a empty string in the querystring. "text"should be the variable i pass in my query when i'm writing.
perfect it works..the "problem" is still the calls..every time i type something starts a call so i get even 10 calls in queue. have you got a solution maybe using a promise or something other method?
Updated the answer. I commented the added lines.
Ahahah now it aborts every call :) from first to last word i type in my input text
SOLVED! thank you. I created a function with inside promiseCanceller.resolve("user cancelled"); promiseCanceller = $q.defer(); and i call it just before http call. It works!
|

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.