Call function from the controller which is belonging to another

旧城冷巷雨未停 提交于 2019-12-25 06:57:20

问题


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

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

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

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

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(){
    //
}

my indes.xml

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

my keyboard.js

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

exports.test = function (){
}

回答1:


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

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

Alternatively what I usually do:

index.js

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

keyboard.js

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


来源:https://stackoverflow.com/questions/22019972/call-function-from-the-controller-which-is-belonging-to-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!