1
0
mirror of synced 2024-11-25 14:26:03 +03:00

simplified support for local array data

This commit is contained in:
Igor Vaynberg 2012-03-28 14:32:18 -07:00
parent f4e0089b1f
commit d7f0c0a8b1

View File

@ -298,37 +298,63 @@
query.callback(data); query.callback(data);
}); });
} else { } else {
if (!("query" in opts) && opts.ajax) { if (!("query" in opts)) {
opts.query = (function () { if ("ajax" in opts) {
var timeout, // current scheduled but not yet executed request opts.query = (function () {
requestSequence = 0, // sequence used to drop out-of-order responses var timeout, // current scheduled but not yet executed request
quietMillis = opts.ajax.quietMillis || 100; requestSequence = 0, // sequence used to drop out-of-order responses
quietMillis = opts.ajax.quietMillis || 100;
return function (query) { return function (query) {
window.clearTimeout(timeout); window.clearTimeout(timeout);
timeout = window.setTimeout(function () { timeout = window.setTimeout(function () {
requestSequence += 1; // increment the sequence requestSequence += 1; // increment the sequence
var requestNumber = requestSequence, // this request's sequence number var requestNumber = requestSequence, // this request's sequence number
options = opts.ajax, // ajax parameters options = opts.ajax, // ajax parameters
data = options.data; // ajax data function data = options.data; // ajax data function
data = data.call(this, query.term, query.page); data = data.call(this, query.term, query.page);
$.ajax({ $.ajax({
url: options.url, url: options.url,
dataType: options.dataType, dataType: options.dataType,
data: data data: data
}).success( }).success(
function (data) { function (data) {
if (requestNumber < requestSequence) { if (requestNumber < requestSequence) {
return; return;
}
query.callback(options.results(data, query.page));
} }
query.callback(options.results(data, query.page)); );
} }, quietMillis);
); };
}, quietMillis); }());
}; } else if ("data" in opts) {
}()); opts.query = (function () {
var data = opts.data, // data elements
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)) text = function (item) { return item[data.text]; };
data = data.results;
}
return function (query) {
var t = query.term.toUpperCase(), filtered = {};
if (t === "") {
query.callback({results: data});
return;
}
filtered.result = $(data)
.filter(function () {return text(this).toUpperCase().indexOf(t) >= 0;})
.get();
query.callback(filtered);
};
}());
}
} }
} }
if (typeof(opts.query) !== "function") { if (typeof(opts.query) !== "function") {