typeahead.js - Bloodhound identify bug? -
i'm using latest version of typeahead.js (v0.11.1). observed strange behaviors when using different ids dataset values.
i've created jsfiddle. here's js code:
var ds = new bloodhound({ datumtokenizer: bloodhound.tokenizers.obj.whitespace('name'), querytokenizer: bloodhound.tokenizers.whitespace, local: [{id: 1, name: "a b 1"}, {id: 2, name: "a b 2"}, {id: 3, name: "a"}], identify: function(obj) { return obj.id; } }); $('#go').typeahead(null, { name: 'ds', display: 'name', source: ds });
now typeahead may malfunction if change data 'local'. here examples:
using 1 of these values 'local' (notice third element random numbers starting '1'):
[{id: 1, name: "a b 1"}, {id: 2, name: "a b 2"}, {id: 15, name: "a"}]
[{id: 1, name: "a b 1"}, {id: 2, name: "a b 2"}, {id: 1849, name: "a"}]
now when enter text box: "a b", typeahead expected suggest "a b 1" , "a b 2", in fact suggests "a b 1".
this can fixed 1 of these:
change 'id' property of third element value doesn't start '1'. example:
[{id: 1, name: "a b 1"}, {id: 2, name: "a b 2"}, {id: 23, name: "a"}]
change 'name' property of third element value doesn't start 'a'. example:
[{id: 1, name: "a b 1"}, {id: 2, name: "a b 2"}, {id: 15, name: "s"}]
remove 'identify' property of bloodhound constructor object.
what's more, if use number greater 2 id of first element, this:
[{id: 3, name: "a b 1"}, {id: 2, name: "a b 2"}, {id: 15, name: "a"}]
now when enter "a b" text box, there's no suggestion!
answering own question. yes there bug in bloodhound. searchindex.getintersection() function incorrectly implemented. can take out function , test this:
getintersection([1,2,15],[1,2])
it supposed return [1,2] result, in fact returns [1]. because uses sort() function incorrectly sort numbers. according w3schools:
by default, sort() method sorts values strings in alphabetical , ascending order.
this works strings ("apple" comes before "banana"). however, if numbers sorted strings, "25" bigger "100", because "2" bigger "1".
so function can fixed changing these 2 lines:
arraya = arraya.sort(); arrayb = arrayb.sort();
into:
arraya = arraya.sort(function(a, b){return a-b}); arrayb = arrayb.sort(function(a, b){return a-b});
took me 1 day find out :(
Comments
Post a Comment