我试图模仿 Appcelerator 钛合金中的事件指示器模块。 它工作正常,但我不明白 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>
我需要在另一个 View 中使用此文件,即dashboard.xml
在dashboard.js Controller 中,我使用$.loadIndicator.show()和$.loadIndicator.hide()函数。
仪表板.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 不是一个函数。隐藏功能也是如此。
我不明白的是这两行是如何使隐藏和显示功能可访问的。这两行的可能等效代码是什么。
这里的$指的是什么?
After going through other widgets I get the convention that, if you require a widget in View instead of Controller then using $.variable sets it as visible to outside world. Same way as module.exports sets it visible to outside world.
如果我错了,请纠正我。
最佳答案
$.hide = hide;
$
读取名为 $
的变量的值。
.hide
(假设 value 是一个对象,否则会出现错误)访问名为 hide
的属性。
= hide
获取本地 hide
变量的值(这是同名函数,提升,因为它是使用函数声明)并将其分配给该属性。
下一行的工作方式相同,只是名称不同。
What I do not understand is how those two lines are making hide and show function accessible.
或者:
- 代码第一位中
$
变量的值的对象与后面$.loadIndicator
的值的对象相同。 - 其他一些代码再次复制了这些函数
And what can be possible equivalent code of those two lines.
为什么需要不同的代码来做同样的事情?
关于javascript - 不明白钛合金 Controller 中$.variableName = functionName的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47453271/