From 657d0863c30db5f425f132ee9ddc21fa9f9140de Mon Sep 17 00:00:00 2001 From: Nicolas KERMARC Date: Sun, 3 Jun 2012 17:57:35 +0300 Subject: [PATCH 1/2] Accept a function as source. Function needs to call the callback, which is the first parameter. source:function(callback) { mydata = [1,2]; callback(mydata); } --- jqm.autoComplete-1.3.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/jqm.autoComplete-1.3.js b/jqm.autoComplete-1.3.js index ecf5a55..b4d0c62 100755 --- a/jqm.autoComplete-1.3.js +++ b/jqm.autoComplete-1.3.js @@ -79,6 +79,16 @@ return re.test(element_text); }); buildItems($this, data, settings); + } + // Accept a function as source. + // Function needs to call the callback, which is the first parameter. + // source:function(callback) { mydata = [1,2]; callback(mydata); } + else if (typeof settings.source === 'function') { + + settings.source(function(data){ + buildItems($this, data, settings); + }); + } else { $.get(settings.source, { term: text }, function(data) { buildItems($this, data, settings); From 5f7f0e98d338d7fe47b8759bc543d9fb0749d736 Mon Sep 17 00:00:00 2001 From: Nicolas KERMARC Date: Fri, 15 Jun 2012 18:29:57 +0300 Subject: [PATCH 2/2] Add text parameters to be able to query an API with the input value Example : -------- $('#searchField').autocomplete({ target : $('#mytarget'), source:function(text,callback) { $.ajax({ url:'http://my.api.com/search?q='+text, success:function(data) { // We need to build objects readable by the library (label field etc) // We assume here that data is an array of objects which have 'title' and 'id' attributes, we iterate over it var tabReturn = new Array() $.each(data,function(index,cur) { tabReturn.push({ label:cur.title, value:cur.id }) }) callback(tabReturn) } }) --- jqm.autoComplete-1.3.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jqm.autoComplete-1.3.js b/jqm.autoComplete-1.3.js index b4d0c62..226b2b4 100755 --- a/jqm.autoComplete-1.3.js +++ b/jqm.autoComplete-1.3.js @@ -82,10 +82,10 @@ } // Accept a function as source. // Function needs to call the callback, which is the first parameter. - // source:function(callback) { mydata = [1,2]; callback(mydata); } + // source:function(text,callback) { mydata = [1,2]; callback(mydata); } else if (typeof settings.source === 'function') { - settings.source(function(data){ + settings.source(text,function(data){ buildItems($this, data, settings); });