jQuery插件:可公开访问的功能?

时间:2011-02-03 03:23:01

标签: javascript jquery plugins tags

我正在修改jQuery插件。

该插件是一个标签系统,基于jQuery ui的自动完成功能进行自动完成。

目前似乎没有办法(除解析创建的列表项外)找出已选择的标签。

我修改了插件,以便管理一个名为tags的数组,其中包含列表。

但是现在我需要一种方法来获取阵列。

初始化你打电话

$('#id').tagit({availableTags: 'tags.php'});

我希望能够做的就是调用

之类的东西

$('#id').tagit('tags');$('#id').tagit().tags();

获取标签列表。

如何修改此代码以添加该功能?

(function($) {

    $.fn.tagit = function(options) {

        var tags = [];

        var defaults = {
            availableTags: [],
            allowSpace:    false
        };

        var options = $.extend(defaults, options);

        var el = this;

        const BACKSPACE = 8;
        const ENTER = 13;
        const SPACE = 32;
        const COMMA = 44;

        // add the tagit CSS class.
        el.addClass("tagit");

        // create the input field.
        var html_input_field = "<li class=\"tagit-new\"><input class=\"tagit-input\" type=\"text\" /></li>\n";
        el.html(html_input_field);

        tag_input = el.children(".tagit-new").children(".tagit-input");

        $(this).click(function(e) {
            if (e.target.tagName == 'A') {
                // Removes a tag when the little 'x' is clicked.
                // Event is binded to the UL, otherwise a new tag (LI > A) wouldn't have this event attached to it.
                $(e.target).parent().remove();
            }
            else {
                // Sets the focus() to the input field, if the user clicks anywhere inside the UL.
                // This is needed because the input field needs to be of a small size.
                tag_input.focus();
            }
        });

        tag_input.keydown(function(event) {
            if (event.which == BACKSPACE) {
                if (tag_input.val() == "") {
                    // When backspace is pressed, the last tag is deleted.
                    tags.pop();
                    $(el).children(".tagit-choice:last").remove();
                }
            }
            // Comma/Space/Enter are all valid delimiters for new tags.
            else if (event.which == COMMA || (event.which == SPACE && !options.allowSpace) || event.which == ENTER) {
                event.preventDefault();

                var typed = tag_input.val();
                typed = typed.replace(/,+$/, "");
                typed = typed.trim();

                if (typed != "") {
                    if (is_new(typed)) {
                        create_choice(typed);
                    }
                    // Cleaning the input.
                    tag_input.val("");
                }
            }
        });

        tag_input.autocomplete({
            source: options.availableTags,
            select: function(event, ui) {
                if (is_new(ui.item.value)) {
                    create_choice(ui.item.value);
                }
                // Cleaning the input.
                tag_input.val("");

                // Preventing the tag input to be update with the chosen value.
                return false;
            }
        });

        function is_new(value) {
            if (value in oc(tags))
                return false;
            return true;
        }

        function create_choice(value) {
            var el = "";
            el = "<li class=\"tagit-choice\">\n";
            el += value + "\n";
            el += "<a class=\"tagit-close\">x</a>\n";
            el += "<input type=\"hidden\" style=\"display:none;\" value=\"" + value + "\" name=\"item[tags][]\">\n";
            el += "</li>\n";
            var li_search_tags = this.tag_input.parent();
            $(el).insertBefore(li_search_tags);
            this.tag_input.val("");
            tags.push(value);
        }

        function oc(a) {
            var o = {};
            for (var i = 0; i < a.length; i++) {
                o[a[i]] = '';
            }
            return o;
        }
    };

    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, "");
    };

})(jQuery);

3 个答案:

答案 0 :(得分:0)

不确定这是否有效,但您可以在$.fn.tagit内尝试:

el.tags = function () {
    return tags;
}

然后应返回tags数组。

答案 1 :(得分:0)

最不突兀的方法是将tags作为数据添加到元素上:

// Inside the plugin
var tags = [];
this.data('tagit-tags', tags);

然后使用它:

// Initialise, as you had before
$('#id').tagit({availableTags: 'tags.php'});
// Get tags
var tags = $('#id').data('tagit-tags');

答案 2 :(得分:0)

更多的努力,但尝试在jQuery UI中构建您的插件 - 它将在各个组件上维护状态,在创建后提供公共方法,并发布事件。这很容易使用:

http://jqueryui.com/docs/Developer_Guide

您的获取代码看起来像这样:

$('#id').tagit("getTags");
相关问题