0

I am trying to search form that will invoke and return ajax call only if the parameter contains 6 or more characters.

In my controller, I have a simple function.

function getByParam(param) {
    if (param.length >= 6) {
        return $http.get('https://example.com/endpoint/' + param);
    } else {
        return noItems();
    }
}

The trouble is with returning a promise with an empty response set. Whatever I seem to try results in an error.

myController.getByParam(param).success is not a function

Currently I have this as the noItems() function.

function noItems() {

    var promise = Promise(function(resolve, reject) {

        if (true) {
            resolve();
        } else {
            reject();
        }
    }).then(function(){
        var emptyResponse = {success:true, response:{records:[]}}
        return emptyResponse
    });

    return promise
}

I have seen answers involving $httpBackend but this seems like overkill. I have also seen suggestions using $timeout but again, I'm missing something to get it to work.

3
  • What have you tried? Have you met any problems with spyOn('$http, 'get')? $httpBackend is not overkill but the recommended way of HTTP mocking in Angular. Commented Apr 24, 2016 at 15:14
  • My issue is that it something this trivial shouldn't really require mocking a http request. What I am struggling with is how to return a simple promise with output that would be processed the same way. Commented Apr 24, 2016 at 15:16
  • The problem I see here is that noItems uses native Promise (is processed asynchronously), while $http uses $q promise (is processed synchronously and depends on digest cycle). And mocking $http request with Promise won't make it processed in the same way. Please, elaborate on the choice of promise implementation. Commented Apr 24, 2016 at 15:38

1 Answer 1

1

Here I made an example using $q instead of Promise (seems a bit more legible the code Link to the example, also you should try to return an error from the server, if someone makes an http request to your server without using your application it's going to crash, because you were doing the validations of the 6 characters.

If the server does this validation, it should return an error message or something, so you don't have to do that logic on your client side.

I am assuming that the server does not this things and give some advices

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

3 Comments

Yes - the server will return an error, but I would like to completely avoid the overhead of an unnecessary network request.
Ok, you can use my example then, but remember that if that someday the server starts accepting less than 6 parameters you're going to have to change your client side code, that's the problem with having double validations (server and client side), the good thing is that you improve the user experience
$q and your code got part-way there. But $q does not have a success method like $http.get so I had to refactor other parts to use then / catch.

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.