【问题标题】:Did not understand meaning of $.variableName = functionName in Titanium Alloy Controller不明白钛合金控制器中 $.variableName = functionName 的含义
【发布时间】:2017-11-23 10:29:59
【问题描述】:

我试图模仿 Appcelerator Titanium Alloy 中的活动指示器模块。 它工作正常,但我不明白 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>

我在另一个视图中需要这个文件,即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);
}

如果我在activityIndi​​cator.js 中注释掉前两行

$.hide = hide;
$.show = show;

然后它显示 show loadIndicator.show 不是一个函数。隐藏功能也一样。

我不明白的是这两行如何使隐藏和显示功能可访问。以及这两行可能的等效代码。

这里的$指的是什么?

经过其他小部件后,我得到的约定是,如果您需要 View 中的小部件而不是 Controller,则使用 $.variable 将其设置为对外界可见。与 module.exports 设置它对外界可见的方式相同。

如果我错了,请纠正我。

【问题讨论】:

    标签: javascript appcelerator-titanium titanium-alloy


    【解决方案1】:
    $.hide = hide;
    

    $ 读取名为$ 的变量的值。

    .hide(假设 value 是一个对象,否则会出错)访问一个名为 hide 的属性。

    = hide 获取本地 hide 变量的值(这是同名的函数,提升,因为它是使用 函数声明 创建的)并将其分配给该属性。

    下一行的工作方式相同,只是名称不同。

    我不明白的是这两行如何使隐藏和显示功能可访问。

    要么:

    • 代码第一位中$变量值的对象与后面$.loadIndicator值的对象相同。
    • 其他一些代码再次复制了这些函数

    这两行的可能等效代码是什么。

    为什么你需要不同的代码来做同样的事情?

    【讨论】:

    • 感谢您的语法解释,但我不明白如何将属性设置为 $ 可以访问其他控制器中的隐藏功能。 $ 在这里指的是什么? - 在钛加速器方面
    【解决方案2】:

    在 Appceleraor 的 wiki 上找到答案: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 项目中,以在 Alloy 项目的视图中指定的小部件 ID 为前缀调用 init——在本示例中,ID 为 foo:

    $.foo.init({title:'Yes', color:'gray', message:'I pity the foo.'});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多