我试图模仿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]");
})();
&#13;
activityIndicator.xml
<Alloy>
<View id="loadingOverlay" visible="false" zIndex="1">
<ActivityIndicator id="loadingIndicator"/>
</View>
</Alloy>
&#13;
我在另一个视图中需要这个文件,即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);
}
&#13;
如果我在activityIndicator.js
中注释掉前两行
$.hide = hide;
$.show = show;
&#13;
然后它显示show loadIndicator.show不是一个函数。和隐藏功能一样。
我不明白的是这两行是如何使隐藏和显示功能可访问的。什么可能是这两行的等效代码。
$指的是什么?
在浏览了其他小部件之后,我得到了这样的约定,如果你需要View中的小部件而不是Controller,那么使用$ .variable将它设置为对外界可见。与module.exports相同的方式将其设置为对外界可见。
如果我错了,请纠正我。
答案 0 :(得分:0)
$.hide = hide;
$
读取名为$
的变量的值。
.hide
(假设该值是一个对象,否则会出错)访问一个名为hide
的属性。
= hide
获取本地hide
变量的值(这是同名函数,已提升,因为它是使用函数声明<创建的/ em>)并将其分配给该属性。
下一行的工作方式相同,只是在不同名称的东西上。
我不明白的是这两行是如何使隐藏和显示功能可访问的。
或者:
$
变量值的对象是同一个对象,后面是$.loadIndicator
的值。这两行的等效代码是什么。
为什么你需要不同的代码来做同样的事情?
答案 1 :(得分:0)
在Appceleraor的维基上找到答案:creating widgets
小部件控制器中的所有方法都是私有的,除非您在方法前加上$,这使得Alloy项目和其他小部件可以访问它。例如,如果在窗口小部件控制器中定义了以下代码:
$.init = function (args) {
// Button object with id=button
$.button.title = args.title || 'Si';
$.button.color = args.color || 'black';
// global variable
message = args.message || 'Hola mundo';
}
然后,在Alloy项目中,调用init前缀为Alloy项目视图中指定的窗口小部件ID - 在此示例中,ID为foo:
$.foo.init({title:'Yes', color:'gray', message:'I pity the foo.'});