钛合金:从另一个控制器调用控制器功能 [英] titanium alloy: calling controller function from another controller
问题描述
使用SDK 3.2.0
Using SDK 3.2.0
我有一个索引控制器,该控制器定义了用于设置Android菜单的功能。
我想从整个应用程序嵌套加载的各种控制器中调用该函数。
代码:
I have a index controller that defines a function to set Android Menus. I want to call that function from a variety of controllers that are nested loaded throughout the App. Code:
//index.js
exports.setMenus = function(enabled) {
var activity = $.index.getActivity();
activity.onCreateOptionsMenu = function(e){
/...
};
activity.onPrepareOptionsMenu = function(e) {
/...
};
activity.invalidateOptionsMenu();
};
Alloy.Globals.Index = $;
然后,在 home控制器内部
,我尝试这样做:
Then, way after, inside home controller
, I try this:
function startRefresh() {
//...
Alloy.Globals.Index.setMenus(true);
}
$.on('focus', startRefresh);
收到以下错误消息:
[ERROR] : TiExceptionHandler: (main) [1,40482] - In alloy/controllers/home.js:8,29
[ERROR] : TiExceptionHandler: (main) [0,40482] - Message: Uncaught TypeError: Obje
ct #<Controller> has no method 'setMenus'
[ERROR] : TiExceptionHandler: (main) [0,40482] - Source: Alloy.Globals.Ind
ex.setMenus(true);
我遵循了。我想使用导出,因为调用setMenus的控制器不是索引的子级,而是更深层的嵌套。我的意思是,我试图避免在控制器之间传递参数。
I have followed instructions from this answer. I want to use exports because the controllers calling setMenus are not children of index, they are deeper nested. I mean, I'm trying to avoid passing arguments between controllers.
为什么未导出setMenus?
Why setMenus is not been exported?
工作原理:
//index.js
exports.setMenus = function(enabled) {
var activity = $.index.getActivity();
//...
};
Alloy.Globals.setMenus = setMenus;
和
//home.js
function startRefresh() {
//...
Alloy.Globals.setMenus(true);
}
$.on('focus', startRefresh);
推荐答案
如果要从多种方法中调用函数控制器,然后您可以在 alloy.js
中编写该函数,例如:
If you want to call a function from a variety of controllers then you can write that function in alloy.js
for ex:
Alloy.Globals.someGlobalFunction = function(){
alert( Hello);
};
现在您可以调用该函数,例如 Alloy.Globals.someGlobalFunction() ;
and now you cal call the function anywhere like Alloy.Globals.someGlobalFunction();
这篇关于钛合金:从另一个控制器调用控制器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!