4

私は次のコードを持っています:

<div id="widHolder"></div>
<script type="text/javascript" language="javascript">
    $('#widHolder').widgetName({
        optionOne: false,
        optionTwo: 1,
        onComplete: function (holder) { 
            // ... do something here with the 'widHolder' object such as $(holder).addClass(x,y) 
        }
    });
</script>

ウィジェット自体の中で、onCompleteメソッドはウィジェットの完全な初期化の直後に呼び出されます。ウィジェット内のコードが、ウィジェットがリンクされているオブジェクト(この場合、IDが「widHolder」のdiv)を参照するようにします。

私の目標は、上記のようなoncomplete関数を作成することにより、保持オブジェクトをすばやく簡単に参照できるようにすることです。ウィジェット自体のコードは、onComplete関数を呼び出して、ホルダー(取得する必要があります)をパラメーターとして渡すだけです。

これがjQueryUIウィジェットプラグインのコードサンプルです

(function ($) {
    $.widget("ui.widgetName", {
        options: {
            // ... other options that can be set
            onComplete: undefined
        },

        // called on the initialization of the widget
        _init: function () {
            // do initialization functions...

            if(this.options.onComplete)
                this.options.onComplete( I_WANT_TO_SEND_THE_DOM_ELEMENT_HERE );
        },
    }
})
4

2 に答える 2

7

jQuery UI Widget Factoryは、次の方法で要素を利用できるようにします。

this.elementプラグインから。

詳細については、こちらをご覧ください:http ://wiki.jqueryui.com/w/page/12138135/Widget%20factory

それはあなたの質問にもっとよく答えますか?

(function ($) {
    $.widget("ui.widgetName", {
        options: {
            // ... other options that can be set
            onComplete: undefined
        },

        // called on the initialization of the widget
        _init: function () {
            // do initialization functions...

            if(this.options.onComplete)
                this.options.onComplete( this.element );
        },
    }
})

補足-プラグイン内に別のクロージャーを作成する場合は、への参照を保存する必要がありますthis。例えば:

(function ($) {
    $.widget("ui.widgetName", {
        options: {
            // ... other options that can be set
            onComplete: undefined
        },

        // called on the initialization of the widget
        _init: function () {
            // do initialization functions...
            var self = this;
            $.each(an_array_or_something_obj, function(){
                //here this will refer to the current element of the an_array_or_something_obj
                //self will refer to the jQuery UI widget
            });
            if(this.options.onComplete)
                this.options.onComplete( this.element );
        },
    }
})
于 2012-01-23T01:52:26.940 に答える
0

this.offsetParent例のように、所有者がウィジェットの直接の親である場合に使用します。

于 2012-01-23T02:31:04.987 に答える