我需要使用从 JSON 响应返回的新数据更新 win1.title(标签)。我可以在我的控制台 win1.title 值上打印,但我不能为其分配新值!
应用程序.js
var win1 = Titanium.UI.createWindow({
title:'Tab 1',
backgroundColor: 'black',
layout: 'vertical',
url: 'win1.js',
title: 'Loading...',
artist: '' });
win1.open();
//Fetching data
var jsonData = ''; var pointer = 0;
var url = "http://example.com"; var xhr = Ti.Network.createHTTPClient({
onload: function(e) {
jsonData = JSON.parse(this.responseText).response.songs;
//HERE I NEED TO UPDATE win1.title with title returned by JSON
/*
if a print win1.title it works correctly.
console.log(win1.title);
but if I try to assign data to win1.title nothing happens, even a error!
*/
win1.addEventListener('swipe', function(e) {
console.log("win1 title:" + win1.title); win1.title = jsonData[pointer].title; win1.artist = jsonData[pointer].artist_name; win1.image = jsonData[pointer].tracks[0].release_image;
});
},
onerror: function(e) {
console.log(e);
},
timeout:20000 /* in milliseconds */ }); xhr.open("GET", url); xhr.send(); // request is actually sent with this statement
win1.js
(function() {
var win1 = Ti.UI.currentWindow;
var image = Ti.UI.createImageView({
image:win1.image,
top: 40
});
var title = Ti.UI.createLabel({
color: 'white',
font: { fontSize:38 },
text: win1.title,
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
top: 20,
width: 'auto', height: 'auto'
});
var artist = Ti.UI.createLabel({
color: 'white',
font: { fontSize:28 },
text: win1.artist,
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
top: 30,
width: 'auto', height: 'auto'
});
win1.add(title);
win1.add(artist);
win1.add(image);
})();