Appcelerator/Javascript Using Variable as Routine Syntax Question
Really a javascript question, but here's my code........I would truly appreciate some help. Been too long since I've been in the coding trenches. Here's my code.
I want to make loadtravelurl into a single function I can call from multiple places.
These three lines below are my issues. win2.title = title; win2.add(tv); tab2.open(webwin);
I want to be able to pass variables in loadelectronicsurl function, where I can change the text win2 in win2.title and win2.add(tv) dynamically. Also want to do the same with tab2. I know it can be done, just had a few years off and forgot how to do so in javascript.
Here is the full code, thanks so much for the help in advance!
function loadelectronicsurl(){
var WindowWidth = Ti.Platform.displayCaps.platformWidth;
var WindowHeight = Ti.Platform.displayCaps.platformHeight;
var xhr = Titanium.Network.createHTTPClient();
xhr.open('GET',electronicsurl);
var data = [];
var WindowWidth = Ti.Platform.displayCaps.platformWidth;
xhr.onload = function() {
//Ti.API.info(this.responseText);
var xml = this.responseXML;
var channel = xml.documentElement.getElementsByTagName("channel");
var title = channel.item(0).getElementsByTagName("title").item(0).text;
win2.title = title;
var items = xml.documentElement.getElementsByTa开发者_运维技巧gName("item");
for (var i=0;i<items.length;i++) {
var this_post_title = items.item(i).getElementsByTagName("title").item(0).text;
var post_link = items.item(i).getElementsByTagName("link").item(0).text;
var row = Ti.UI.createTableViewRow({height:'auto',width:WindowWidth,top:0,hasChild: false});
var post_title = Ti.UI.createLabel({
text: this_post_title,
textAlign:'left',
left:0,
height:40,
font:{fontFamily:'Arial',fontSize:12},
width:'auto',
top:3,
color: 'black'
});
row.add(post_title);
row.link = post_link;
data.push(row);
}
var tv = Titanium.UI.createTableView({
data:data,
top:0,
width:WindowWidth,
height:WindowHeight
});
win2.add(tv);
tv.addEventListener('click',function(e) {
var webwin = Titanium.UI.createWindow({
url: 'showweb.js',
backgroundColor: '#fff',
myurl: e.rowData.link
});
tab2.open(webwin);
});
};
xhr.send();
}
You can define parameters for a function like this:
function someFunc(win, tab){
win.add();
tab.open();
}
You can also use the parameters to access properties of an object dynamically like this:
function someFunc(win, tab){
obj.[win](); //To call an object's method
obj.[tab]; //To access an object's property
}
Call the function and pass the paramters:
someFunc(someObject, anotherObject);
someObject
becomes win
and anotherObject
becomes tab
inside the function.
精彩评论