0

I have this code in JS which works very well.

  var values = new Bloodhound({
      datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.num); },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      remote: '/url/that/returns/json'
   });

But, since I only need to this once, I changed it to a prefetch, as below:

  var values = new Bloodhound({
      datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.num); },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      prefetch: {url: '/url/that/returns/json'}
   });

This causes an error ("TypeError: a is undefined") in typeahead.js. What do I need to change to get this functionality working?

EDIT: The json returned is if I visit the URL in my browser is:

[{"name":"MyName","id":"100","code":"CODE"}]

Dhaulagiri
  • 3,281
  • 24
  • 26
Jeremy
  • 5,365
  • 14
  • 51
  • 80

1 Answers1

2

This was caused by the fact that my datumTokenizer was looking for a field called num:

... return Bloodhound.tokenizers.whitespace(d.num)

But there was no such field in my JSON. I have no idea why this worked with remote, but it didn't work in prefetch. The fix was then to replace d.num with d.name.

Jeremy
  • 5,365
  • 14
  • 51
  • 80
  • Good catch! I was trying to implement your JSON and was getting back the numbers I had in my original example. Turns out prefetch uses local storage to reduce the traffic to your webserver (http://jsfiddle.net/2Cres/25/). So watch out if you wind up testing and finding your data is stale. – Brandon Boone Feb 26 '14 at 01:23
  • I noticed that as well, which was a bit frustrating. I started adding random numbers to the end of the URL, which probably explains why it went up to 25 instances :) – Jeremy Feb 26 '14 at 01:27
  • You can change your prefetch settings to ttl: 0 which will always pull a fresh data set – Carey Estes Jul 03 '14 at 19:04
  • I am having this same problem. Can you post an example of your json file, the tokenizer and the filter in prefetch? I am still unclear on how the naming works. – Carey Estes Jul 03 '14 at 19:13