From 6278e7cca09adbc2ffa27e3c22bfb87f7ad9578c Mon Sep 17 00:00:00 2001
From: Lee Baker <lee.rekab@gmail.com>
Date: Sat, 16 Feb 2013 11:56:17 +1100
Subject: [PATCH] Fixing when the data helper is a function for the local query

Signed-off-by: Igor Vaynberg <igor.vaynberg@gmail.com>
---
 select2.js | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/select2.js b/select2.js
index eb8caee7..71458e0d 100644
--- a/select2.js
+++ b/select2.js
@@ -406,25 +406,26 @@ the specific language governing permissions and limitations under the Apache Lic
             tmp,
             text = function (item) { return ""+item.text; }; // function used to retrieve the text portion of a data item that is matched against the search
 
-        if (!$.isArray(data)) {
-            text = data.text;
-            // if text is not a function we assume it to be a key name
-            if (!$.isFunction(text)) {
-              dataText = data.text; // we need to store this in a separate variable because in the next step data gets reset and data.text is no longer available
-              text = function (item) { return item[dataText]; };
-            }
-        }
-
-        if ($.isArray(data)) {
+		 if ($.isArray(data)) {
             tmp = data;
-            data = {results:tmp};
+            data = { results: tmp };
         }
-
-        if ($.isFunction(data) === false) {
+		
+		 if ($.isFunction(data) === false) {
             tmp = data;
             data = function() { return tmp; };
         }
 
+        var dataItem = data();
+        if (dataItem.text) {            
+            text = dataItem.text;
+            // if text is not a function we assume it to be a key name
+            if (!$.isFunction(text)) {
+                dataText = data.text; // we need to store this in a separate variable because in the next step data gets reset and data.text is no longer available
+                text = function (item) { return item[dataText]; };
+            }
+        }	
+
         return function (query) {
             var t = query.term, filtered = { results: [] }, process;
             if (t === "") {
@@ -452,7 +453,7 @@ the specific language governing permissions and limitations under the Apache Lic
                 }
             };
 
-            $(data()).each2(function(i, datum) { process(datum, filtered.results); });
+            $(data().results).each2(function(i, datum) { process(datum, filtered.results); });
             query.callback(filtered);
         };
     }