久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將類附加到 jQuery 對象

attaching a class to a jQuery object(將類附加到 jQuery 對象)
本文介紹了將類附加到 jQuery 對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在為如何最好地結合 javascript 類和 jQuery 插件而苦苦掙扎.這個問題不是很具體,我希望的是指向更多資源的指針.

I'm struggling with how best to combine javascript Classes and jQuery plugins. This question isn't very specific, what I'm hoping for is pointers to more resources.

基本上,我想將狀態數據和私有方法存儲在一個類中,然后擴展我調用插件的每個 jQuery 對象以具有這些私有方法和屬性.這樣我就可以在插件內部直接調用 jQuery 對象的方法.

Basically, I want to store state data and private methods in a class, and then extend each jQuery object which I call my plugin on to have those private methods and properties. Such that inside the plugin I can call methods directly off the jQuery object.

我閱讀了 jQuery 插件設計模式(常見做法?)用于處理私有函數,特別是大衛的回答,但是每次都會初始化一個新類,因此不能用于保存對象的狀態.

I read jQuery plugin design pattern (common practice?) for dealing with private functions, specifically David's answer, however this initializes a new Class each time, and thus can't be used to save the state of the object.

我還發現 http://fuelyourcoding.com/jquery-plugin-design-patterns-part-i/,建議創建一個類,然后將其存儲在 .data() 中.

I also found http://fuelyourcoding.com/jquery-plugin-design-patterns-part-i/, which recommends creating a class and then storing it in .data().

我認為理想情況下我想要的代碼看起來像

I think ideally what I want to end up with is code that looks like

(function( $ ){

  var methods = {
    init : function( options ) { // Initialize each object with a state and private methods },
    show : function( ) { 
      // testFoo() is a private method that checks the element's state
      if(this.testFoo()){
        // Relying on jQuery's html() method
        this.html() = this.fooTemplate();
      }
    }
  };

  // Boiler plate plugin from http://docs.jquery.com/Plugins/Authoring
  $.fn.myPlugin = function( method ) {
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.myPlugin' );
    }    
  };
})( jQuery );

最后,我似乎無法將私有方法直接烘焙到插件中,因為像testFoo()"這樣的方法將返回一個布爾值,因此不可鏈接.

Finally, it doesn't seem like I can bake the private methods into the plugin directly because methods like "testFoo()" will return a boolean, and therefore aren't chainable.

想法?我以正確的方式接近這個嗎?我應該使用另一種設計模式嗎?也許根本不使用 jQuery 插件架構?

Thoughts? Am I approaching this the right way? Is there another design pattern I should be using? Perhaps not using jQuery plugin architecture at all?

推薦答案

這是一個建議的解決方案.它結合了幾種不同的方法(John Resig 的繼承模型和 Alex Saxton 的插件繼承模型).

Here's a proposed solution. It combines few different approaches (John Resig's inheritance model and Alex Saxton's plugin inheritance model).

定義您的可繼承插件:

(function ($) {

    My.Plugin = Class.extend({

        /*
        * Initialization (constructor)
        */
        init: function (element, meta) {
            var $meta = $.extend({ name: "pluginName" }, meta);

            // Call the base constructor
            this._super(element, $meta);

            // TODO: Add custom initialization code like the following:
            // this._testButton = $('.testButton', element).get(0);
        },


        /*
        * Public methods
        */

        show: function() {
             alert('This is a public method');

        },


        /*
        * Private methods
        */

        // DEMO: Overriding the base _paint method:
        _paint: function () {

            // "this._super()" is available in all overridden methods
            // and refers to the base method.
            this._super();

            alert('TODO: implement myPlugin._paint!');
        }


    });


    // Declare this class as a jQuery plugin
    $.plugin('my_plugin', My.Plugin);


})(jQuery);

定義基類

(function () {
    var initializing = false, fnTest = /xyz/.test(function () { xyz; }) ? /_super/ : /.*/;
    // The base Class implementation (does nothing)
    this.Class = function () { };

    // Create a new Class that inherits from this class
    Class.extend = function (prop) {
        var _super = this.prototype;

        // Instantiate a base class (but only create the instance,
        // don't run the init constructor)
        initializing = true;
        var prototype = new this();
        initializing = false;


        // Copy the properties over onto the new prototype
        for (var name in prop) {
            // Check if we're overwriting an existing function
            prototype[name] =
                   typeof prop[name] == "function"
                && typeof _super[name] == "function"
                && fnTest.test(prop[name])
                    ? (function (name, fn) {
                        return function () {
                            var tmp = this._super;

                            // Add a new ._super() method that is the same method
                            // but on the super-class
                            this._super = _super[name];

                            // The method only need to be bound temporarily, so we
                            // remove it when we're done executing
                            var ret = fn.apply(this, arguments);
                            this._super = tmp;

                            return ret;
                        };
                    })(name, prop[name])
                    : prop[name];
        }

        // The dummy class constructor
        function Class() {
            // All construction is actually done in the init method
            if (!initializing && this.init)
                this.init.apply(this, arguments);
        }

        // Populate our constructed prototype object
        Class.prototype = prototype;

        // Enforce the constructor to be what we expect
        Class.constructor = Class;

        // And make this class extendable
        Class.extend = arguments.callee;

        return Class;
    };
})();

插件創建

(function ($) {

    //  The "inheritance plugin" model
    //  [http://alexsexton.com/?p=51][1]

    $.plugin = function (name, object) {
        $.fn[name] = function (options) {
            var instance = $.data(this, name, new object(this, options));
            return instance;
        };
    };
})(jQuery);

從 javascript 調用你的插件:

Calling your plugin from javascript:

$('#someElem').my_plugin({options: {}, data: {} /* you can modify your plugin code to accept anything *
                
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 夜操 | 午夜久久久久久久久久一区二区 | 日韩av一区二区在线观看 | re久久| 久久亚洲高清 | 欧美日韩国产三级 | 久视频在线 | 人人性人人性碰国产 | 精品日韩在线 | 男女污网站 | 久久综合av| 一区二区三区四区不卡 | 日日操夜夜操天天操 | 日韩色视频 | 三级特黄特色视频 | 伦理片97| 成人网在线观看 | 国产欧美精品一区二区三区 | 国产色网站 | 久久综合久色欧美综合狠狠 | 亚洲永久字幕 | 在线免费观看a级片 | 国产一级片 | 91看片官网 | 99国内精品久久久久久久 | 亚洲精彩免费视频 | 亚洲视频在线播放 | 久久综合久久综合久久综合 | 国产精品资源在线 | 国产美女久久 | a免费视频 | 日韩在线视频观看 | 特黄特色大片免费视频观看 | 亚洲免费在线观看 | 久久久久电影 | 日韩一级免费大片 | 欧美aaaaa | 91精品导航 | 欧美国产日韩精品 | 亚洲国产成人精品久久久国产成人一区 | 欧美操操操 |