0

I'm programming a SPA with AngularJS. Right now, I have an input field that triggers a query with $http to a 3rd party API. The search is done on real time as I have a $watch bind with the input model.

My problem is that for whatever reason, the results will only appear when the full string is consistent with the result, not before.

For example, if I'm looking for Frank Sinatra, some results will appear when Frank is typed into the input, but once I'm at Frank Sin, completely unrelated results will appear (or results consistent with Sin, but not with the whole string, and the desired result, will only appear once the whole string is entered.
What I understand, is that the input won't treat the entered text as a whole, but as a sum of the different words, which makes the query missbehave.

I'm attaching my JS, maybe I did something wrong!

angular.module('myApp', ['ngResource'])

function Ctrl($scope, $http) {
var search = function(name) {
  if (name) {
    $http.get('http://api.discogs.com/database/search?type=artist&q='+ name +'&page=1&per_page=5').
      success(function(data3) {
        $scope.clicked = false;
        $scope.results = data3.results;
      });
  }
  $scope.reset = function () {
    $scope.sliding = false;
    $scope.name = undefined;
  }
}
$scope.$watch('name', search, true);
Eric Mitjans
  • 2,149
  • 5
  • 40
  • 69
  • Since you are watching a variable that is associated w/a text input, your watch function will get executed on every key stroke. That means you are sending requests to search for "F", then "Fr", then "Fra", and so on. If this is your intent, then the "problem" is with the API that you're using to search. Alternatively, you could get rid of the watch and add a button to the page. Then use ng-click="search()" on the button (and modify the search function to use the model value). – Sunil D. Mar 16 '14 at 01:04
  • Yup, I saw that in the responses in the console, but when searching for "Frank Sin", I don't quite get why wouldnt offer Frank Sinatra as a result, but rather something else that has no Frank in it :S – Eric Mitjans Mar 16 '14 at 01:10

1 Answers1

1

Try replacing spaces with +

Edit:

That's how their own search on discogs.com is used, there must be something implemented differently (aka broken) in their API.

Edit2: I do get some 'Frank Sinatra' results when I lose the type, querying this:

http://api.discogs.com/database/search?q=frank+sin

Maybe their type handler for the API assumes things about the query string.

Mosho
  • 7,099
  • 3
  • 34
  • 51