]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Alter typeahead to accept synchronous/asynchronous data source via function/callback 3250/head
authorMatt Morgan <mlmorg@gmail.com>
Thu, 26 Apr 2012 18:09:20 +0000 (14:09 -0400)
committerMatt Morgan <mlmorg@gmail.com>
Thu, 26 Apr 2012 18:09:20 +0000 (14:09 -0400)
docs/templates/pages/javascript.mustache
js/bootstrap-typeahead.js
js/tests/unit/bootstrap-typeahead.js

index 1ae3ffa5a82506cf09fd4191a1b00654be579937..e6f15768908e5124ccc3bcca8174108193276b19 100644 (file)
@@ -1385,9 +1385,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>
@@ -1426,4 +1426,4 @@ $('.carousel').carousel({
           <p>{{_i}}Initializes an input with a typeahead.{{/i}}</p>
         </div>
       </div>
-    </section>
\ No newline at end of file
+    </section>
index 95a0fcdb78357d6c6e7e51739810ec3e7421adf6..281bdd6b3e0d20739abbe391a1fc90f2c012279b 100644 (file)
@@ -77,9 +77,7 @@
     }
 
   , lookup: function (event) {
-      var that = this
-        , items
-        , q
+      var items
 
       this.query = this.$element.val()
 
         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)
       })
 
     })
   })
 
-}(window.jQuery);
\ No newline at end of file
+}(window.jQuery);
index 4e2428d6a49143479922537208a09e5ef7deaec1..25d4cafd8e1809d2996aad7a3e98cf7ccab5608c 100644 (file)
@@ -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+']