javascript - 不明白钛合金 Controller 中$.variableName = functionName的含义

标签 javascript appcelerator-titanium titanium-alloy

我试图模仿 Appcelerator 钛合金中的事件指示器模块。 它工作正常,但我不明白 2 行的工作原理。

activityIndi​​cator.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]");
})();

activityIndi​​cator.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);
}

<小时/>

如果我注释掉 ActivityIndi​​cator.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/

相关文章:

javascript - 如何打包我的 React hook 效果供其他人重复使用?

ios - 如何将图像放在appcelerator的单列中

ios - Appcelerator Tabgroup 更改为下拉

javascript - Three.js EventDispatcher可以用来在类之间进行通信吗?

javascript - 如何将我的 JavaScript 文件包含到 HTML 页面中?

javascript - Canvas 填充反射(reflect)在两个形状上

ios - 钛合金 : Saving a View as image to the photoGallery?

ios - #appcelerator 在 5.0 和 ios8.4 中开发的应用程序不再在 ios9.2 和 appcelerator 5.1.1 上运行

node.js - 为什么 Appcelerator Titanium 终端与我的操作系统终端不同?

ios - Titanium : build error after upgrade to 3. 5.0.GA(体系结构 x86_64 的 undefined symbol )