javascript - Appcelerator钛合金MVC : how is '$' useful?

标签 javascript variables titanium titanium-alloy

框架的设置是为了让您可以同时加载多个 View (以及可能关联的 Controller ),但我在编写“知道”所有内容的正确 Controller 代码时遇到了困难。

Titanium 文档描述了“$”的用法,如下所示:

All UI elements which have an id attribute in a view are automatically defined and available as a property prefixed by the special variable $ in the controller. The $ is a reference to the controller. ... To access external controllers and views, use the Alloy.createController and Controller.getView methods, respectively.

Titanium 文档还说,通常 View 和 Controller 是成对工作的。

假设我定义了一个包含“后退”按钮的 View ,以及另一个刷新按钮的 View 。关键是这些 View 包含关联的 Controller ,无论我处于什么上下文中,这些 Controller 都需要工作。后退按钮需要始终关闭顶级窗口,刷新按钮需要重新加载主视图的数据,无论“主视图是什么” '我正在看。

因此,我创建了一个主视图,并将这两个 View 加载到其中。现在我有 3 个 View Controller 在一个上下文中一起工作。逻辑要求后退按钮知道如何关闭其父级,甚至根据您“返回”的位置执行其他操作;刷新按钮显然需要知道如何访问父 View 的数据。如果我总是局限于单个 Controller ,如何使用“$”进行编码?对我来说,当所有 3 个实体都在同一个地方工作时,加载“外部” Controller 或 View 是没有意义的。

最佳答案

以下是如何在不同 Alloy Controller 之间使用 $ 的示例。

index.js:

$.open.addEventListener('click', function() {
    var view = Alloy.createController(view, { title: 'First Title' });
    view.setTitle('Second Title');
});

$.index.open();

View .js:

var args = arguments[0] || {};

$.setTitle = function(title) {
    $.title.text = title;
};

if (args.title) {
    $.setTitle(args.title);
}

当然,我假设index.xml和view.xml存在并且与此类似:

index.xml:

<Alloy>
    <Window>
        <Button id="open" title="Open New Window" />
    </Window>
</Alloy>

View .xml:

<Alloy>
    <Window>
        <Label id="title">Empty Title</Label>
    </Window>
</Alloy>

关于javascript - Appcelerator钛合金MVC : how is '$' useful?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21892003/

相关文章:

javascript - Light DOM 样式泄漏到 Shadow DOM 中

javascript - 重新定义一个 jQuery 对话框按钮

javascript - Plotly Js Choropleth Iso2 而不是 Iso3

javascript - 在点击 jquery 事件中找不到变量

android - 无法解析 Titanium 中的推送有效负载消息

security - 移动到服务器API安全

javascript - gentelella Angular 管理面板 - gentelella 切换菜单和侧边栏单击事件不起作用

php - 添加到 grid.php 文件的网格选择

javascript - 如何将 tabGroup 放入窗口或查看 Titanium Mobile

javascript - 如何使用AJAX从php获取变量