钛合金:从不同的控制器访问UI? [英] Titanium Alloy: Accessing UI from different controllers?

查看:60
本文介绍了钛合金:从不同的控制器访问UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法在Titanium Appcelerator Alloy中更新对象,

I seem to be having trouble updating objects in Titanium Appcelerator Alloy,

我基本上希望能够将表行添加到我当前所处的其他控制器/视图中的表中……..希望以下内容可以更好地描述这一点:s

I basically want to be able to add a table row to a table that is in a different controller/view that i am currently in..... hopefully the below will better describe this :s

basket.xml

basket.xml

<Alloy>
    <Window id="basketWindow" class="container">
        <TableView id="basketTable" />
        <Button id="addItemButton" onClick="addItem">Add Item</Button>
    </Window>
</Alloy>

basket.js

basket.js

function addItem()
{
    var itemList = Alloy.createController('item_list');

    itemList.getView().open();
}

item_list.xml

item_list.xml

<Alloy>
    <Window id="itemListWindow" class="container">
        <TableView id="itemListTable">
            <TableViewRow id="item1" className="item" onClick="addItemToBasket">
                Test Item
            </TableViewRow>
        </TableView>
    </Window>
</Alloy>

item_list.js

item_list.js

function addItemToBasket()
{
    var row = Ti.UI.createTableViewRow({title: 'Test Item'});

    // Here i would ideally want to put something like $.basketTable.append(row);
    // But nothing happens, im guessing it cant find $.basketTable as its in a different controller?

}

有人知道吗?

感谢您的阅读:)

推荐答案

一个简单,简单的解决方案是在将商品添加到购物篮中时触发应用范围的事件:

One simple, easy solution is to just trigger an app wide event when you add an item to the basket:

function addItemToBasket() {
    Ti.App.fireEvent("app:itemAddedToBasket", {
       title : "Test Item", 
       otherAttribute : "Value"
    });
}

然后在购物篮控制器中的某处监听事件,并添加表行:

Then listen for the event in the basket controller somewhere, and add the table row:

// inside basket.js
Ti.App.addEventListener("app:itemAddedToBasket", function(e) {
    // object 'e' has all the row information we need to create the row
    var row = Ti.UI.createTableViewRow({
        title: e.title, 
        otherAttribute: e.otherAttribute
    });
    // Now append it to the table
    $.basketTable.append(row);
});

这篇关于钛合金:从不同的控制器访问UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆