Skip to content

Commit 6278e7c

Browse files
lee-bakerivaynberg
authored andcommitted
Fixing when the data helper is a function for the local query
Signed-off-by: Igor Vaynberg <igor.vaynberg@gmail.com>
1 parent 7d908da commit 6278e7c

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

select2.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -406,25 +406,26 @@ the specific language governing permissions and limitations under the Apache Lic
406406
tmp,
407407
text = function (item) { return ""+item.text; }; // function used to retrieve the text portion of a data item that is matched against the search
408408

409-
if (!$.isArray(data)) {
410-
text = data.text;
411-
// if text is not a function we assume it to be a key name
412-
if (!$.isFunction(text)) {
413-
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
414-
text = function (item) { return item[dataText]; };
415-
}
416-
}
417-
418-
if ($.isArray(data)) {
409+
if ($.isArray(data)) {
419410
tmp = data;
420-
data = {results:tmp};
411+
data = { results: tmp };
421412
}
422-
423-
if ($.isFunction(data) === false) {
413+
414+
if ($.isFunction(data) === false) {
424415
tmp = data;
425416
data = function() { return tmp; };
426417
}
427418

419+
var dataItem = data();
420+
if (dataItem.text) {
421+
text = dataItem.text;
422+
// if text is not a function we assume it to be a key name
423+
if (!$.isFunction(text)) {
424+
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
425+
text = function (item) { return item[dataText]; };
426+
}
427+
}
428+
428429
return function (query) {
429430
var t = query.term, filtered = { results: [] }, process;
430431
if (t === "") {
@@ -452,7 +453,7 @@ the specific language governing permissions and limitations under the Apache Lic
452453
}
453454
};
454455

455-
$(data()).each2(function(i, datum) { process(datum, filtered.results); });
456+
$(data().results).each2(function(i, datum) { process(datum, filtered.results); });
456457
query.callback(filtered);
457458
};
458459
}

0 commit comments

Comments
 (0)