Hi
I hope someone could help as I have scoured the web and yet to find a clear solution to this simple issue I'm having.
-I am using a list view and making a template - I am loading json from a web service and populating the list view with items.
This all works great. What I am struggling with is how I can "store" and retrieve an "id" in a list item from the api. So I can use this open a new view to call specific data.
My alloy markup
<Alloy> <Window class="container" onOpen="doopen"> <ListView id="postList"> <Templates> <ItemTemplate name="rowtemplate" class="rowtemplate"> <ImageView bindId="imagePreview" class="imagePost"></ImageView> <Label bindId="userName" class="userName"/> <Label bindId="postDate" class="postDate"/> </ItemTemplate> </Templates> <ListSection name="posts"> </ListSection> </ListView> </Window> </Alloy>
My controller
var moment = require('alloy/moment'); function doopen(evt){ var data = { items: 8 }; var dataPackage = JSON.stringify(data); var url = "http://[urlhere]&json=" + dataPackage; var json; var xhr = Ti.Network.createHTTPClient({ onload: function() { var data=[]; json = JSON.parse(this.responseText); for (i = 0; i < json.length; i++) { var row = { userName: json[i].added_by, postDate: moment(json[i].date_created).format('ll'), imagePreview: json[i].thumbnail, id: json[i].id, // THIS IS THE ID I WANT TO PASS TO THE LIST ITEM template: "rowtemplate" }; data.push(row); Ti.API.info(json[i].added_by); } var listItems = _.map(data, function(item){ return{ userName :{text: item.userName}, postDate:{text: item.postDate}, imagePreview:{image: item.imagePreview}, template: "rowtemplate" }; }); $.postList.addEventListener('itemclick', function(e){ //var section = e.sectionIndex; var item = $.postList.getItemAt(e.itemIndex); //Ti.API.info("id" + item.id); }); $.postList.sections[0].setItems(listItems); } }); xhr.open("GET",url); xhr.send(); } $.index.open();
So what I would like to do is add the id i am adding to the row array and somehow add that to my list item So that when an item is clicked it can get to the value of that id.
Any pointers would be great in best practice for this
thanks in advance
ade