jquery插件編寫模版

jquery插件是什麼??這裏以討論實力方法爲主,好比 $("div").pluginname({});javascript

他的最簡單形勢應該是java

$.prototype.plugin = function(){}


咱們一點點來加東西

1,本身的變量不污染全局,別人的變量不污染咱們jquery

(function($,undefined){
var window = Function("return this")();//必定是window
$.prototype.plugin = function(){ } 
)(jQuery)

  

2,判斷已加載或者處理其餘附加數據,處理參數,實例化一個對象,實例化的方式不必定要new一個,你喜歡也能夠拷貝一個,工廠一個等。。app

        function plugin(element, options) {
            var self = this;
            self.element = element;
            self.$element = $(element);
            if (typeof options == "object") {
                self.opts = $.extend({}, defaults, options);
            }
        }
        $.fn[pluginName] = function (options, callback) {
            var dataname = "plugin_" + pluginName;
            $(this).each(function (index, item) {
                var hasObject = $(item).data(dataname);
                if (!hasObject) {
                    var someobj = new plugin(item, options);
                    $(item).data(dataname, someobj);
                }
            });
        }
        return $.fn[pluginName];

  3,AMD CMD加載jquery插件

    // 全局模式 
    var pluginobj = factory(jQuery);
    //UMD
    if (typeof exports === 'object') {
        module.exports = pluginobj;
    }
    //AMD
    if (typeof define === "function" && define.amd) {
        define(pluginName, ["jquery"], function () {
            return pluginobj;
        });
    }
    //CMD
    if (typeof define === "function" && define.cmd) {
        define(pluginName, ['jquery'], function (require, exports, module) {
            var $ = require('jQuery');
            module.exports = pluginobj;
        });
    }

  全局模式是否開放取決於你的依賴項是否必然加載。ui

此時把原來的自執行外面再套一層,把原來的自執行改爲普通方法更名爲factory方法。this

 
完整版:
(function () {
    var window = Function("return this")();
    var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../..";
    var pluginName = "mypluginname";

    function factory($) {
        "use strict";
        if ($.isFunction($.fn[pluginName])) {
            return $.fn[pluginName];
        }

        function plugin(element, options) {
            var self = this;
            self.element = element;
            self.$element = $(element);
            if (typeof options == "object") {
                self.opts = $.extend({}, defaults, options);
            }
        }
        $.fn[pluginName] = function (options, callback) {
            //something like old jquery plugin
            var dataname = "plugin_" + pluginName;
            $(this).each(function (index, item) {
                var hasObject = $(item).data(dataname);
                if (!hasObject) {
                    var someobj = new plugin(item, options);
                    $(item).data(dataname, someobj);
                }
            });
        }
        return $.fn[pluginName];;
    }

    var loaded = false;

    //UMD
    if (typeof exports === 'object') {
        module.exports = factory();

    }
    //AMD
    if (typeof define === "function" && define.amd) {
        define(pluginName, ["jquery"], factory);
        loaded = true;
    }
    //CMD
    if (typeof define === "function" && define.cmd) {
        define(pluginName, ['jquery'], function (require, exports, module) {
            var $ = require('jQuery');
            module.exports = factory($);
        });
    }
    // other
    if (!jQuery.xxx && jQuery.loadScript) {
        $.loadScript("/scripts/plugins/xxx.js", function () {
            pluginobj = factory(jQuery);
        })
    }
    if (!loaded) {
        // 全局模式 也能夠不判斷強制加載是執行一遍全局模式
        factory(jQuery);
    }
})();
相關文章
相關標籤/搜索