代码之家  ›  专栏  ›  技术社区  ›  mukesh.kumar

不理解$的含义。variableName=钛合金控制器中的functionName

  •  1
  • mukesh.kumar  · 技术社区  · 8 年前

    我试图在Appcelerator钛合金中模拟活动指示模块。 它工作得很好,但我不理解2条线的工作原理。

    活动指示器。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]");
    })();

    活动指示器。xml

    <Alloy>
      <View id="loadingOverlay" visible="false" zIndex="1">
        <ActivityIndicator id="loadingIndicator"/>
      </View>
    </Alloy>

    我在另一个视图即dashboard中需要此文件。xml

    和仪表板中。我使用的是js控制器$。负荷指示器。show()和$。负荷指示器。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不是一个函数。隐藏函数也是如此。

    我不明白的是这两行是如何使隐藏和显示功能变得可访问的。这两行可能的等价代码是什么。

    这里的$指的是什么?

    在浏览了其他小部件之后,我得到了一个惯例,即如果您需要视图中的小部件而不是控制器,那么使用$。变量将其设置为对外部世界可见。与模块相同。导出使其对外部世界可见。

    如果我错了,请纠正我。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Quentin    8 年前
    $.hide = hide;
    

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

    .hide (假设该值是一个对象,否则它将是一个错误)访问一个名为 hide .

    = hide 取局部 隐藏 变量(即同名函数, 已吊装 因为它是使用 函数声明 )并将其分配给该财产。

    下一行以同样的方式工作,只是在不同名称的事物上。

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

    要么:

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

    为什么需要做相同事情的不同代码?

        2
  •  0
  •   mukesh.kumar    8 年前

    在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.'});