Titanium Alloy: Accessing UI from different controllers? - titanium

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

Related

Titanium: Animate an element in a ListItem/ ListView

I want to animate an element in a ListItem
For example, consider the following simple ListView:
<ListView>
<Templates>
<ItemTemplate name="foo">
<View layout="vertical">
<Label color="red" id="label" bindId="bExampleLabel"/>
<Button onClick="onClickButton">Click Me to make the label go blue</Button>
</View>
</ItemTemplate>
</Templates>
<ListSection id="exampleListSection">
<ListItem template="foo" bExampleLabel:text="Example 1"></ListItem>
<ListItem template="foo" bExampleLabel:text="Example 2"></ListItem>
</ListSection
</ListView>
and the following script:
function onClickButton(e) {
var item = $.exampleListSection.getItemAt(e.itemIndex);
item.bExampleLabel = {
color: 'blue'
};
$.exampleListSection.updateItemAt(e.itemIndex, item);
}
The above XML code simply has a ListView which contains 2 ListItem, which each contains a label and a button. When you click the button, it makes the label go blue.
However I want it so that it animates it to blue.
Usually this is done like so:
$.elementId.animate({
color: 'blue'
});
However, I do not know how to do this in the context of a ListItem as you cannot seem to access the objects directly.
you can't animate ListItem in listView, you can change only the item properties accessing with bindId
templates : Dictionary
Contain key-value pairs mapping a style name (key) to an ItemTemplate (value).
This property cannot be changed once a window has been opened.
if you want to set animation in listing use TableView, and you can do what you want with TableViewRow

react bootstrap tooltip, how do I fire it?

Have added a tooltip and trigger to my component, how can i get it to fire the tooltip?
required modules like this:
var Icon = require('./Icon'),
Tooltip = require('./Tooltip'),
Button = require('react-bootstrap').Button,
OverlayTrigger = require('react-bootstrap').OverlayTrigger;
getTooltip: function() {
return <Tooltip text="sample text" />;
},
Rendering trigger like this:
<OverlayTrigger placement="right" overlay={this.getTooltip()}>
<Button bsStyle="default">Button text to trigger</Button>
</OverlayTrigger>
But nothing happens?
How can I get this to fire?
Thanks
Tooltip component is:
var TooltipBS = require('react-bootstrap').Tooltip;
var Tooltip = React.createClass({
render: function() {
return (
<TooltipBS>
{this.props.text}
</TooltipBS>
)
}
});
module.exports = Tooltip;
I assume you are using react-bootstrap tooltip?
If you read the documentation here http://react-bootstrap.github.io/components.html#tooltips-in-text you can see how to use it in your code.
<OverlayTrigger placement="right" overlay={this.getTooltip()} trigger="click">
<Button bsStyle="default">Button text to trigger</Button>
</OverlayTrigger>
You can add preferred trigger event with trigger property.
It should be:
getTooltip: function() {
return <Tooltip id="tooltip">sample text</Tooltip>;
},
I believe you will need a render() within the component class. Then you will need a ReactDOM.render(<Tooltip />, document.getElementById('app'); outside of the class.

Get values from textfields in tablerow titanium

Hi i'm a beginner with titanium and i would like to get the value from textfields
in a tablerow
my view
<Alloy>
<Collection src="field"/>
<Window id="addWin" title="Add Name" class="container" modal="true">
<TableView id="textfield" dataCollection="field">
<TableViewRow>
<TextField class="insertField" hintText="{field_description}"/>
</TableViewRow>
</TableView>
<Button onClick="addForm">Add form</Button>
</Window>
And my controller
function addForm() {
while (fieldlist.isValidRow())
{
var field_description = fieldlist.fieldByName('field_description');
if(field_description == 'name') {
var contact = Alloy.createModel('contact', {
name : $.insertField.value,
});
}
fieldlist.next();
}
contacts.add(contact, {silent:true});
contact.save();
closeWindow();
}
I need to filter my insertField.value to get just one textfield from my form but i don't know how to do it. It return something like Cannot read property 'value' of undefined.
I think i need to loop it but i don't how.
Thanks if you help me
Well if you have the view file static ( as pasted by you ) , I will suggest to add an id to the TextField.
Something like :
<TextField class="insertField" id="myTextField" hintText="{field_description}"/>
Then get the value of TextField as :
var myTextFieldValue = $.myTextField.getValue();

Titanium JS: Close all windows under an iOS NavigationWindow and return to the root window

In Titanium JS, I'm using the newer NavigationWindow component, and you can traverse several windows down into a navigation tree, creating a series of back buttons as you go.
However, I also have a side revealing menu with a "home" option. I'd like this button to take you back to the root window in the NavigationWindow.
Here is a simple example. Imagine that the new window being created could happen several times and you could be several windows into the navigation tree.
In my view
<Alloy>
<NavigationWindow id="navWin" class="container">
<Window title="Window 1">
<Button onClick="newWindow" title="Go to new window" />
</Window>
</NavigationWindow>
</Alloy>
And in my controller:
function newWindow() {
var newWin = Ti.UI.createWindow({ title: "Window 2" });
var goBackBtn = Ti.UI.createButton({ title: "Go to root" });
newWin.add(goBackBtn);
goBackBtn.addEventListener("click", function () {
// goes back to the root window no matter how deep into the navigation tree you are
});
$.navWin.openWindow(newWin); }
$.navWin.open();
Use navAlloy controller for coming to home window
https://github.com/vuinguyen/NavControlTi32
Thanks

Data binding does not work after simple modal popup

Please consider the following code (also in this fiddle):
var viewModel = {
count:ko.observable(0),
add:function (){
this.count(this.count()+1);
},
popup:function (){
$.modal($("#divPopup"));
}
}
ko.applyBindings(viewModel);
And this corresponding View:
<button id="btnAdd" data-bind="click:add">Add</button>
<button id="btnPopup" data-bind="click:popup">Popup</button>
<div id="divPopup">
<span data-bind="text:count"></span>
</div>
Now:
click Add button
click Popup button
click top right corner of modal window (sorry I can't have "x" image)
Add button don't work
I can't use:
$.modal($("#divPopup").html());
Because in my app html does not render when $.modal().
Or to put it as another question: how I can know when html render was completed when my viewModel changed?
Try passing persist: true in for the options to modal() like:
$("#divPopup").modal({ persist: true });
http://jsfiddle.net/rniemeyer/BxVF9/