从属于另一个控制器的调用函数 [英] Call function from the controller which is belonging to another

查看:25
本文介绍了从属于另一个控制器的调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控制器键盘"属于索引"控制器

I have two controllers 'keyboard' is belonging to 'index' controller

我知道我可以从 index.js 调用 keyboard.js 中的函数.

I know I can call function in keyboard.js from index.js.

但是如何从keyboard.js调用index.js中的函数??

But How can I call the function in index.js from keyboard.js??

在这种情况下,我想从 fromhere() 函数调用 wantToCall()

in this cae I would like to call wantToCall() from fromhere() function

我的 index.js

my index.js

var KeyboardCon = Alloy.createController('keyboard',{});
$.KeyboardView.add(KeyboardCon.getView());  
KeyboardCon.test() // I can call the function in keyboard.js from index.js

function wantToCall(){
    //
}

我的 indes.xml

my indes.xml

<Alloy>
<Window id="GameWin" class="container">
    <View id="KeyboardView" />
</Window>
</Alloy>

我的键盘.js

function fromhere(){
   I want to call wanToCall from here.
}

exports.test = function (){
}

推荐答案

我相信这对你有用.您将 $ 传递给键盘控制器以传递引用,以便它可以引用键盘中的索引控制器.抱歉没有时间测试.

I believe that will do it for you. You pass the $ into the keyboard controller to pass a reference so it can refer to the index controller in keyboard. Sorry didn't have time to test it.

index.js

var KeyboardCon = Alloy.createController('keyboard',{index: $});
...    
exports.wantToCall = function(){
    //
}

keyboard.js

keyboard.js

var args = arguments[0] || {};
function fromhere(){
   args.index.wantToCall();  
}

或者我通常做的事情:

index.js

Alloy.Globals.Index = $;
...
exports.wantToCall(){
        //
    }

keyboard.js

keyboard.js

function fromhere(){
   Alloy.Globals.Index.wantToCall();  
}

这篇关于从属于另一个控制器的调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆