我在我的 Controller (ControllerA
) 中,并且有一个外部文件,我想要处理方向更改。
//-- In ControllerA
var gestures = require('gestures');
在 gestures.js
中,我需要访问 $
,以便可以操作 ControllerA
中的一些元素
在 gestures.js
中,我目前未定义 $
我已经成功地通过在gestures
中创建一个init()
函数来使其工作,并且我可以实例化需求,例如:
vargestures = require('gestures').init($);
但这感觉像是一个黑客。
在合金中执行此操作的正确方法是什么?
编辑
只是一个旁注。我也尝试使用 Ti.include()
执行此操作,但同样的事情...无法访问 $
最佳答案
CommenJS 模块(使用 require 包含在其他 Controller 中)必须独立于任何其他 Controller 。从它的名称“gesture.js”来看,我认为您正在尝试控制一些方向变化和摇动手势...您必须定义此模块才能在所有其他 Controller 中使用它,并且为此功能存在。
例如假设我有一个名为animations.js的模块:
var animations={};
animations.moveToLeft=function(element,newLeftValue){
var animation=Ti.UI.createAnimation({
left:newLeftValue,
duration:700
... so on
});
element.animate(animation);
};
module.exports=animations;
然后我可以使用许多其他 Controller 的这个模块并为许多钛的物体设置动画 只需在controller.js文件中需要它并使用moveToLeft函数...
关于javascript - 使用 Alloy 访问所需文件中的 $,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26921249/