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

本文是小编为大家收集整理的关于钛合金。从不同的控制器访问用户界面?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我似乎在钛合金Appcelerator合金中更新对象

我基本上希望能够将表行添加到我目前在不同的控制器/视图中的表中.....希望下面更好地描述:s

basket.xml

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

basket.js

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

    itemList.getView().open();
}

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

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?

}

有谁知道这一点?

谢谢阅读:)

推荐答案

一个简单,简单的解决方案是在将项目添加到篮子时触发应用程序宽事件:

function addItemToBasket() {
    Ti.App.fireEvent("app:itemAddedToBasket", {
       title : "Test Item", 
       otherAttribute : "Value"
    });
}
然后在某处侦听篮子控制器中的事件,然后添加表行:

// 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);
});

本文地址:https://www.itbaoku.cn/post/1938174.html

问题描述

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

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

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

basket.js

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

    itemList.getView().open();
}

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

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?

}

Does anyone know away around this?

Thanks for reading :)

推荐答案

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);
});