سؤال

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"}]

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top