1

たとえば、XUI 用のプラグインを作成しました。

xui.extend({
    myPlugin : function(){

        var options = [];

        function methodOne(obj){

        }

        function methodTwo(){

        }
    }
});

そして今、私は呼び出すことができます:

<script type="text/javascript">
xui.ready(function(){

    // call my plugin on an XUI object
    x$('element').myPlugin();

});
</script>

しかし、プラグイン内のいくつかのメソッドにアクセスできるようにする必要もあります (このコードは機能しませんが、私が達成しようとしていることを示しています)。

<script type="text/javascript">
xui.ready(function(){

    // call my plugin on an XUI object
    x$('element').myPlugin();

    var foo = bar;

    // call one of my methods?
    xui.fn.myPlugin.methodOne(foo);

});
</script>

XUI モバイル フレームワークの経験が豊富な方は、どなたか手を貸していただけないでしょうか? 乾杯

編集 **

以下は機能しますが、おそらくあまりエレガントではありません...

xui.extend({
    myPlugin : function(){

        // create an API object
        var api = {
            'publicOne' : function(obj){
                return methodOne(obj)
            }
        };

        // add our API object to prototype
        xui.fn.myPlugin.api = api;

        /**
        * the functions of the plugin
        **/

        // we'll expose this in the API object
        function methodOne(obj){
            // Do something with obj...
        }

        function methodTwo(){

        }
    }
});

そして、ページでこれを使用できます:

<script type="text/javascript">
xui.ready(function(){

    // call my plugin on an XUI object, which does its thing
    x$('element').myPlugin();

    // arbitary
    var foo = x$('#bar');

    // call one of the methods statically
    xui.fn.myPlugin.api.pluginOne(foo);

});
</script>

目的を説明するために、私は JQTouch に非常によく似たものを作成していますが、もちろん巨大な jQuery に依存せず、XUI の上に座っています。「ページ」遷移を処理するページ ナビゲーションを処理するメソッドがありますが、プログラムでもメソッドをトリガーできるようにする必要があります。

上記は他の XUI 関係者の役に立つかもしれません。現在は私のために機能していますが、改善される可能性はありますか?

4

1 に答える 1

0

リアムもっと役に立つのは、おそらくあなたがそれをしたい理由を理解することですか?コールバックを介していつでも関数を公開できます。

xui.extend({
    myPlugin : function(callback){

        var options = [];

        function methodOne(obj){

        }

        function methodTwo(){

        }

        if(typeof callback === 'function') {
            return callback(this);
        }
    }
});

次に、次のように呼び出します。

<script type="text/javascript">
xui.ready(function(){

    // call my plugin on an XUI object
    x$('element').myPlugin(function(myPublic) {
        var foo = bar;

        // call one of my methods?
        myPublic.methodOne(foo);
    });
});
</script>

これはテストされていませんが、機能するはずです。

于 2011-08-12T14:09:29.663 に答える