Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter typeahead to accept synchronous/asynchronous data source #3682

Merged
merged 2 commits into from
Jun 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/templates/pages/javascript.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -1376,9 +1376,9 @@ $('.carousel').carousel({
<tbody>
<tr>
<td>{{_i}}source{{/i}}</td>
<td>{{_i}}array{{/i}}</td>
<td>{{_i}}array, function{{/i}}</td>
<td>[ ]</td>
<td>{{_i}}The data source to query against.{{/i}}</td>
<td>{{_i}}The data source to query against. May be an array of strings or a function. The function is passed two arguments, the <code>query</code> value in the input field and the <code>process</code> callback. The function may be used synchronously by returning the data source directly or asynchronously via the <code>process</code> callback's single argument.{{/i}}</td>
</tr>
<tr>
<td>{{_i}}items{{/i}}</td>
Expand Down Expand Up @@ -1417,4 +1417,4 @@ $('.carousel').carousel({
<p>{{_i}}Initializes an input with a typeahead.{{/i}}</p>
</div>
</div>
</section>
</section>
16 changes: 11 additions & 5 deletions js/bootstrap-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,23 @@
}

, lookup: function (event) {
var that = this
, items
, q
var items

this.query = this.$element.val()

if (!this.query) {
return this.shown ? this.hide() : this
}

items = $.grep(this.source, function (item) {
items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source

return items ? this.process(items) : this
}

, process: function (items) {
var that = this

items = $.grep(items, function (item) {
return that.matcher(item)
})

Expand Down Expand Up @@ -290,4 +296,4 @@
})
})

}(window.jQuery);
}(window.jQuery);
36 changes: 36 additions & 0 deletions js/tests/unit/bootstrap-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ $(function () {
typeahead.$menu.remove()
})

test("should accept data source via synchronous function", function () {
var $input = $('<input />').typeahead({
source: function () {
return ['aa', 'ab', 'ac']
}
})
, typeahead = $input.data('typeahead')

$input.val('a')
typeahead.lookup()

ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
equals(typeahead.$menu.find('.active').length, 1, 'one item is active')

typeahead.$menu.remove()
})

test("should accept data source via asynchronous function", function () {
var $input = $('<input />').typeahead({
source: function (query, process) {
process(['aa', 'ab', 'ac'])
}
})
, typeahead = $input.data('typeahead')

$input.val('a')
typeahead.lookup()

ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
equals(typeahead.$menu.find('.active').length, 1, 'one item is active')

typeahead.$menu.remove()
})

test("should not explode when regex chars are entered", function () {
var $input = $('<input />').typeahead({
source: ['aa', 'ab', 'ac', 'mdo*', 'fat+']
Expand Down