【问题标题】:Exposing public methods in XUI JS framework plugin?在 XUI JS 框架插件中公开公共方法?
【发布时间】:2011-10-25 19:53:42
【问题描述】:

我已经为 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 人,它目前对我有用,但也许可以改进?

【问题讨论】:

    标签: javascript mobile javascript-framework xui


    【解决方案1】:

    Liam 更有用的可能是了解您想要这样做的原因?您始终可以通过回调公开函数。

    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>
    

    这是未经测试的,但应该可以工作。

    【讨论】:

    • 实际上很有用,因为某些 mu 方法确实需要添加回调,但我没有在描述中提供帮助...我需要能够调用其中的一些方法插件是静态的,即不在对象上。我会编辑我的答案,因为我可能已经找到了一种方法,虽然它并不优雅......
    • 您可以在 xui.extend 之外编写 myPlugin 函数,然后将其传递给 xui.extend(myPlugin)。这样你就可以单独使用它并作为插件使用,我仍然不明白为什么你需要在插件之外访问它。
    • 刚刚编辑了我原来的问题,但现在你的建议很有意义!
    猜你喜欢
    • 2011-02-09
    • 2011-11-23
    • 2013-02-26
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2013-08-08
    • 2011-04-19
    相关资源
    最近更新 更多