Skip to content

Commit 2f6e8c5

Browse files
author
Gilbert Boyreau
committed
Add support for typeahead multiple datasets
1 parent 8e042dd commit 2f6e8c5

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/bootstrap-tagsinput.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -341,25 +341,20 @@
341341

342342
// typeahead.js
343343
if (self.options.typeaheadjs) {
344-
var typeaheadConfig = null;
345-
var typeaheadDatasets = {};
346344

347-
// Determine if main configurations were passed or simply a dataset
345+
// Determine if main configurations were passed or simply a dataset
348346
var typeaheadjs = self.options.typeaheadjs;
349-
if ($.isArray(typeaheadjs)) {
350-
typeaheadConfig = typeaheadjs[0];
351-
typeaheadDatasets = typeaheadjs[1];
352-
} else {
353-
typeaheadDatasets = typeaheadjs;
347+
if (!$.isArray(typeaheadjs)) {
348+
typeaheadjs = array( null, typeaheadjs);
354349
}
350+
var valueKey = typeaheadjs[1].valueKey; // We should test typeaheadjs.size >= 1
351+
var f_datum = valueKey ? function (datum) { return datum[valueKey]; }
352+
: function (datum) { return datum; }
353+
$.fn.typeahead.apply(self.$input,typeaheadjs).on('typeahead:selected', $.proxy(function (obj, datum) {
354+
self.add( f_datum(datum) );
355+
self.$input.typeahead('val', '');
356+
}, self));
355357

356-
self.$input.typeahead(typeaheadConfig, typeaheadDatasets).on('typeahead:selected', $.proxy(function (obj, datum) {
357-
if (typeaheadDatasets.valueKey)
358-
self.add(datum[typeaheadDatasets.valueKey]);
359-
else
360-
self.add(datum);
361-
self.$input.typeahead('val', '');
362-
}, self));
363358
}
364359

365360
self.$container.on('click', $.proxy(function(event) {

0 commit comments

Comments
 (0)