diff options
author | Matt Morgan <mlmorg@gmail.com> | 2012-06-03 08:33:06 +0400 |
---|---|---|
committer | Matt Morgan <mlmorg@gmail.com> | 2012-06-03 08:33:06 +0400 |
commit | af14f6db5dddd15556b2c932e29ea1fc7a811594 (patch) | |
tree | 8bb846859d4e4335cd52d5b6fc7eb154c2d9a643 | |
parent | 91a7274a43836484cdf27e34c45856e4b715f778 (diff) | |
parent | bf9d8fcc070fa826c342282e102fe00e1af76d10 (diff) |
Merge branch 'async' into 2.1.0-wip
-rw-r--r-- | docs/templates/pages/javascript.mustache | 6 | ||||
-rw-r--r-- | js/bootstrap-typeahead.js | 16 | ||||
-rw-r--r-- | js/tests/unit/bootstrap-typeahead.js | 36 |
3 files changed, 50 insertions, 8 deletions
diff --git a/docs/templates/pages/javascript.mustache b/docs/templates/pages/javascript.mustache index 26ca9b538d..7e0fdd8587 100644 --- a/docs/templates/pages/javascript.mustache +++ b/docs/templates/pages/javascript.mustache @@ -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> @@ -1417,4 +1417,4 @@ $('.carousel').carousel({ <p>{{_i}}Initializes an input with a typeahead.{{/i}}</p> </div> </div> - </section>
\ No newline at end of file + </section> diff --git a/js/bootstrap-typeahead.js b/js/bootstrap-typeahead.js index e5197a3314..6e896bb98b 100644 --- a/js/bootstrap-typeahead.js +++ b/js/bootstrap-typeahead.js @@ -77,9 +77,7 @@ } , lookup: function (event) { - var that = this - , items - , q + var items this.query = this.$element.val() @@ -87,7 +85,15 @@ 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) }) @@ -290,4 +296,4 @@ }) }) -}(window.jQuery);
\ No newline at end of file +}(window.jQuery); diff --git a/js/tests/unit/bootstrap-typeahead.js b/js/tests/unit/bootstrap-typeahead.js index 4e2428d6a4..25d4cafd8e 100644 --- a/js/tests/unit/bootstrap-typeahead.js +++ b/js/tests/unit/bootstrap-typeahead.js @@ -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+'] |