在外部javascript中调用Jquery函数(Jquery Boilerplate插件)

时间:2015-02-10 14:11:55

标签: javascript jquery jquery-plugins

我正在使用https://github.com/jquery-boilerplate/jquery-boilerplate

我在该插件中创建了一个方法“fillLoginForm”并试图在

之外访问它
// the semi-colon before function invocation is a safety net against concatenated
// scripts and/or other plugins which may not be closed properly.
// TODO : Write public methods here above
;
(function ($, window, document, undefined) {

    // undefined is used here as the undefined global variable in ECMAScript 3 is
    // mutable (ie. it can be changed by someone else). undefined isn't really being
    // passed in so we can ensure the value of it is truly undefined. In ES5, undefined
    // can no longer be modified.

    // window and document are passed through as local variable rather than global
    // as this (slightly) quickens the resolution process and can be more efficiently
    // minified (especially when both are regularly referenced in your plugin).

    // Create the defaults once
    var pluginName = "messageDashboard",
        defaults = {
            propertyName: "value",

        };

    // The actual plugin constructor
    function Plugin(element, options) {
        var widget = this;
        widget.element = element;
        // jQuery has an extend method which merges the contents of two or
        // more objects, storing the result in the first object. The first object
        // is generally empty as we don't want to alter the default options for
        // future instances of the plugin

        widget.settings = $.extend({}, defaults, options);
        widget._defaults = defaults;
        widget._name = pluginName;
        widget.initLoginPage(); // FIX : To be decided whether its login page or message dashboard 

        $.each(widget.settings, function (key, value) {
            if (typeof value === 'function') {
                console.log(' adding Handler');
                console.log(' For :' + key);
                console.log(' Value :' + value);
                console.log(widget.element);
                $('body').on(key + '_' + pluginName,
                    function (e) {
                        console.log(' Event Handled On:' + widget.element);
                        value(e, widget.element);
                    }
                );
            }
        });

    }

    // Avoid Plugin.prototype conflicts
    $.extend(Plugin.prototype, {
        /**
         *  To initialize Login Widget
         *
         */
        initLoginPage: function () {
            // Place initialization logic for Login page here
            // You already have access to the DOM element and
            // the options via the instance, e.g. this.element
            // and this.settings
            // you can add more functions like the one below and
            // call them like so: this.yourOtherFunction(this.element, this.settings).



        },

        /**
         *  To initialize Message Dashboard Widget Widget
         *
         */
        initDashboard: function () {
            // Place initialization logic for Login page here
            // You already have access to the DOM element and
            // the options via the instance, e.g. this.element
            // and this.settings


        },

        /**
         *
         * To fill the login form when the remember me option is enabled
         *
         */
       fillLoginForm : function(username, password) {

            $("#id").val(username);
            $("#pwd").val(password);


        }



    });

    // A really lightweight plugin wrapper around the constructor,
    // preventing against multiple instantiations
    $.fn[pluginName] = function (options) {
        this.each(function () {
            if (!$.data(this, "plugin_" + pluginName)) {
                $.data(this, "plugin_" + pluginName, new Plugin(this, options));
            }



        });

        // chain jQuery functions
        return this;
    };

})(jQuery, window, document);

在另一个地方,我正试图像

那样访问它
$('body').on('loadEvent',
        function (e) {
            var $dashboard = $('body').messageDashboard();
            $dashboard.fillLoginForm("id","pwd");
        }
    );

但它会抛出未定义的错误!

或者我如何创建可以直接在外面访问的公共方法?

1 个答案:

答案 0 :(得分:0)

我能够通过使用如下的数据方法来使其工作。

 $('body').on('loadEvent',
        function (e) {
            var $dashboard = $('body').messageDashboard().data('plugin_messageDashboard');
            $dashboard.fillLoginForm("4089032912", "2912");
        }
    );

但我希望这不是解决问题的更好方法!!