【发布时间】:2017-11-23 10:29:59
【问题描述】:
我试图模仿 Appcelerator Titanium Alloy 中的活动指示器模块。 它工作正常,但我不明白 2 行的工作原理。
activityIndicator.js
$.hide = hide; // <= What does these two lines
$.show = show; // <= means. How Iam able to access hide and show functions in dashboard.js controller directly?
function hide () {
$.loadingOverlay.hide();
$.loadingIndicator.hide();
}
function show () {
$.loadingOverlay.show();
$.loadingIndicator.show();
}
(function init(){
Titanium.API.trace("[activityIndicator] >> [init]");
})();
activityIndicator.xml
<Alloy>
<View id="loadingOverlay" visible="false" zIndex="1">
<ActivityIndicator id="loadingIndicator"/>
</View>
</Alloy>
我在另一个视图中需要这个文件,即dashboard.xml
在dashboard.js 控制器中,我使用了$.loadIndicator.show() 和$.loadIndicator.hide() 函数。
dashboard.js
//just the function related to loadIndicator
function serviceFailed(e) {
$.loadIndicator.hide(); //hide function works well.
var errorMessage = Ti.UI.createLabel({
text : "Error loading data!",
color : "red"
});
$.listContainer.add(errorMessage);
alert("Failed:" + e.toString());
}
////just the function related to loadIndicator
function showList() {
$.loadIndicator.show(); //this also works fine.
serviceUtil.doUtilServiceCall(function(resp) {
populateList(resp);
ReceivedData = resp;
Ti.API.info('Data is set to response received.');
}, serviceFailed);
}
如果我在activityIndicator.js 中注释掉前两行
$.hide = hide;
$.show = show;
然后它显示 show loadIndicator.show 不是一个函数。隐藏功能也一样。
我不明白的是这两行如何使隐藏和显示功能可访问。以及这两行可能的等效代码。
这里的$指的是什么?
经过其他小部件后,我得到的约定是,如果您需要 View 中的小部件而不是 Controller,则使用 $.variable 将其设置为对外界可见。与 module.exports 设置它对外界可见的方式相同。
如果我错了,请纠正我。
【问题讨论】:
标签: javascript appcelerator-titanium titanium-alloy