例如,我为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的上面。我有一个处理页面导航的方法,它可以处理“页面”转换,但是我也需要能够以编程的方式触发该方法。
也许上面的这些可以帮助其他的汇人,它目前正在为我工作,但也许还可以改进吗?
发布于 2011-08-12 14:09:29
利亚姆,更有用的可能是理解你想这么做的原因?您可以始终通过回调公开该函数。
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>这是未经测试的,但应该有效。
https://stackoverflow.com/questions/7040787
复制相似问题