Как я могу вызвать подфункцию из объекта функции в JavaScript - PullRequest
3 голосов
/ 30 марта 2011

Я проверил связанные вопросы по переполнению стека, но не могу найти ответ на мое затруднительное положение.Я пытаюсь использовать плагин для JavaScript (Tag it! - Tag Editor), и мне нужно найти способ вызвать одну из его функций «create_choice ()» EDIT: в какой-то момент после он имеетбыл инициирован.Есть ли способ после вызова:

$tagit = $("#mytags").tagit();

, что я могу затем назвать что-то вроде

$tagit.create_choice('test123');

Вот ссылка для примера: http://levycarneiro.com/projects/tag-it/example.html

Нижеэто код из плагина, если это поможет

(function($) {

    $.fn.tagit = function(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.keypress(function(event){
            if (event.which == BACKSPACE) {
                if (tag_input.val() == "") {
                    // When backspace is pressed, the last tag is deleted.
                    $(el).children(".tagit-choice:last").remove();
                }
            }
            // Comma/Space/Enter are all valid delimiters for new tags.
            else if (event.which == COMMA || event.which == SPACE || 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){
            var is_new = true;
            this.tag_input.parents("ul").children(".tagit-choice").each(function(i){
                n = $(this).children("input").val();
                if (value == n) {
                    is_new = false;
                }
            })
            return is_new;
        }
        function create_choice (value){
            var el = "";
            el  = "<li class=\"tagit-choice\">\n";
            el += value + "\n";
            el += "<a class=\"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("");
        }
    };

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

})(jQuery);

Ответы [ 3 ]

2 голосов
/ 30 марта 2011

Я создал рабочий пример на http://jsfiddle.net/nickywaites/DnkBt/, но он требует внесения изменений в плагин.

0 голосов
/ 30 марта 2011

Вы можете попробовать добавить

return this;

сразу после блока функции create_choice.tagit вернет себя, и вы можете вызвать make_choice или любую функцию, содержащуюся в .fn.tagit

0 голосов
/ 30 марта 2011

Изменение

$.fn.tagit = function(options) { ...

до

$.fn.tagit = function(options,callback) { ...

Добавить

if (callback && typeof callback == 'function') {
    callback();
}

после

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

Теперь вы можете вызывать функцию по вашему выбору сразу после вызова tagit:

$tagit = $("#mytags").tagit(yourOptions, function(){
    alert('hi')!
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...