Permalink
Browse files

merged loopj's public methods support

  • Loading branch information...
1 parent 1853d3b commit f38cd5c0e1a3423274f3dae1043966cf923befdc @vdepizzol committed Jul 15, 2011
Showing with 97 additions and 7 deletions.
  1. +30 −0 demo.html
  2. +67 −7 src/jquery.tokeninput.js
View
30 demo.html
@@ -287,5 +287,35 @@ <h2 id="parsename">Using local data with parseName function and searchColumns se
});
</script>
</div>
+
+ <h2 id="plugin-methods">Using the add, remove and clear Methods</h2>
+ <div>
+ <a href="#" id="plugin-methods-add">Add Token</a> | <a href="#" id="plugin-methods-remove">Remove Token</a> | <a href="#" id="plugin-methods-clear">Clear Tokens</a><br />
+ <input type="text" id="demo-input-plugin-methods" name="blah" />
+ <input type="button" value="Submit" />
+ <script type="text/javascript">
+ $(document).ready(function() {
+ $("#demo-input-plugin-methods").tokenInput("http://shell.loopj.com/tokeninput/tvshows.php");
+
+ // Add a token programatically
+ $("#plugin-methods-add").click(function () {
+ $("#demo-input-plugin-methods").tokenInput("add", {id: 999, name: "James was here"});
+ return false;
+ });
+
+ // Remove a token programatically
+ $("#plugin-methods-remove").click(function () {
+ $("#demo-input-plugin-methods").tokenInput("remove", {name: "James was here"});
+ return false;
+ });
+
+ // Clear all tokens
+ $("#plugin-methods-clear").click(function () {
+ $("#demo-input-plugin-methods").tokenInput("clear");
+ return false;
+ });
+ });
+ </script>
+ </div>
</body>
</html>
View
74 src/jquery.tokeninput.js
@@ -103,13 +103,37 @@ var KEY = {
COMMA: 188
};
-// Expose the .tokenInput function to jQuery as a plugin
-$.fn.tokenInput = function (url_or_data_or_function, options) {
- var settings = $.extend({}, DEFAULT_SETTINGS, options || {});
+// Additional public (exposed) methods
+var methods = {
+ init: function(url_or_data_or_function, options) {
+ var settings = $.extend({}, DEFAULT_SETTINGS, options || {});
+
+ return this.each(function () {
+ $(this).data("tokenInputObject", new $.TokenList(this, url_or_data_or_function, settings));
+ });
+ },
+ clear: function() {
+ this.data("tokenInputObject").clear();
+ return this;
+ },
+ add: function(item) {
+ this.data("tokenInputObject").add(item);
+ return this;
+ },
+ remove: function(item) {
+ this.data("tokenInputObject").remove(item);
+ return this;
+ }
+}
- return this.each(function () {
- new $.TokenList(this, url_or_data_or_function, settings);
- });
+// Expose the .tokenInput function to jQuery as a plugin
+$.fn.tokenInput = function (method) {
+ // Method calling and initialization logic
+ if(methods[method]) {
+ return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
+ } else {
+ return methods.init.apply(this, arguments);
+ }
};
@@ -459,6 +483,39 @@ $.TokenList = function (input, url_or_data_or_function, settings) {
hide_dropdown();
}
}
+
+
+ //
+ // Public functions
+ //
+
+ this.clear = function() {
+ token_list.children("li").each(function() {
+ if ($(this).children("input").length === 0) {
+ delete_token($(this));
+ }
+ });
+ }
+
+ this.add = function(item) {
+ add_token(item);
+ }
+
+ this.remove = function(item) {
+ token_list.children("li[data-uniqueid]").each(function() {
+ var currToken = $(this).data("tokeninput");
+ var match = true;
+ for (var prop in item) {
+ if (item[prop] !== currToken[prop]) {
+ match = false;
+ break;
+ }
+ }
+ if (match) {
+ delete_token($(this));
+ }
+ });
+ }
@@ -538,11 +595,14 @@ $.TokenList = function (input, url_or_data_or_function, settings) {
// Add a token to the token list based on user input
function add_token (item) {
+
if(typeof(item) === "string") {
var li_data = {name: item};
- } else {
+ } else if(item[0]) {
var li_data = $.data(item.get(0), "tokeninput");
+ } else {
+ var li_data = item;
}
if(!li_data) {

0 comments on commit f38cd5c

Please sign in to comment.