Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
Added the ability to populate via a function, similar to jQuery UI Au…
Browse files Browse the repository at this point in the history
…tocomplete.

Instead of a string or object, specify a function as the source parameter with two parameters (the query string and callback function respectively). The function can do what it needs to, then call the callback function passing through the query string and data as a JSON object.
  • Loading branch information
alexgorbatchev committed Jun 7, 2011
1 parent 94f4bc2 commit 63296be
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/jquery.tokeninput.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,25 @@ var KEY = {


// Expose the .tokenInput function to jQuery as a plugin
$.fn.tokenInput = function (url_or_data, options) {
$.fn.tokenInput = function (url_or_data_or_function, options) {
var settings = $.extend({}, DEFAULT_SETTINGS, options || {});

return this.each(function () {
new $.TokenList(this, url_or_data, settings);
new $.TokenList(this, url_or_data_or_function, settings);
});
};


// TokenList class for each input
$.TokenList = function (input, url_or_data, settings) {
$.TokenList = function (input, url_or_data_or_function, settings) {
//
// Initialization
//

// Configure the data source
if(typeof(url_or_data) === "string") {
if(typeof(url_or_data_or_function) === "string") {
// Set the url to query against
settings.url = url_or_data;
settings.url = url_or_data_or_function;

// Make a smart guess about cross-domain if it wasn't explicitly specified
if(settings.crossDomain === undefined) {
Expand All @@ -114,9 +114,11 @@ $.TokenList = function (input, url_or_data, settings) {
settings.crossDomain = (location.href.split(/\/+/g)[1] !== settings.url.split(/\/+/g)[1]);
}
}
} else if(typeof(url_or_data) === "object") {
} else if(typeof(url_or_data_or_function) === "function") {
settings.sourceFunction = url_or_data_or_function;
} else if(typeof(url_or_data_or_function) === "object") {
// Set the local data to search through
settings.local_data = url_or_data;
settings.local_data = url_or_data_or_function;
}

// Build class names
Expand Down Expand Up @@ -675,6 +677,7 @@ $.TokenList = function (input, url_or_data, settings) {
// Deselect a token in the token list
function deselect_token (token, position) {
token.removeClass(settings.classes.selectedToken);

selected_token = null;

input_box.css('color', '');
Expand Down

0 comments on commit 63296be

Please sign in to comment.