框架的设置是为了让您可以同时加载多个 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/