问题
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