-2

我有一個代碼,我需要從中獲取圖像。我有一行,我想要一個圖像,我從API調用中獲得。我迷失在如何得到它。通過鈦加速器傳遞變量功能

這是我得到我行

Ti.Facebook.requestWithGraphPath(Titanium.App.Properties.getString("pageID") +'/feed',  {access_token: Token}, 'GET', function(e){ 
    if (e.success) { 

     json = JSON.parse(e.result); 
for (i = 0; i < json.data.length; i++) { 
    data = json.data[i]; 
    row = Ti.UI.createTableViewRow({ 
     height:'80dp', 
     // backgroundImage : 'images/tablebgl.png', 
     backgroundColor: 'transparent', 
     separatorColor:'transparent', 
    }); 
row.coverid = data.from.id;  

var coverid = row.coverid; 

    Titanium.App.Properties.setString("cv", coverid); 

    var imag = Ti.UI.createImageView(); 
row.add(imag); 
require('image').get.signUpConn(data, function(scrollView){ 
imag.setImage(scrollView); 
}); 

這是我的「image.js」

exports.signUpConn = function(data, callback) { 
    var scrollView = Ti.UI.createView({ 
     top : 0, 
     right : -1, 
     left : -1, 
     backgroundColor : 'transparent', 
     width : '95%', 
     contentWidth : 'auto', 
     contentHeight : 'auto', 
     layout : 'horizontal' 
    }); 

    var url = "https://graph.facebook.com/" + Titanium.App.Properties.getString("cv") + "?fields=cover"; 
    var loader = Titanium.Network.createHTTPClient(); 
    loader.onload = function(data) { 
     var data = JSON.parse(this.responseText); 
     var images = []; 
     for (var c = 0; c < 1; c++) { 
      images[c] = { 
       image : data.cover.source, 
       width : 300 
      }; 

     } 
     function square(i) { 
      var view = Ti.UI.createImageView({ 
       image : images[i].image, 
       width : 300, 
       height : 111, 
       top : 5, 
      }); 
      return view; 
     } 

     for (var i = 0; i < 1; i++) { 
      scrollView.add(square(i)); 
     } 

    } 
    callback(scrollView); 
    loader.open("GET", url); 
    loader.send(data); 

} 

所以我想這錯誤必須在功能和/或回調。

圖像將位於'scrollView'中。

當涉及到函數和來回回調時,我是一團糟。

這是錯誤我得到:

Script Error = 'undefined' is not an object (evaluating 'require('image').get.signUpConn') 

感謝名單。

回答

0

你的代碼有多個語法錯誤(Facebook的要求,很多缺少括號)但錯誤的reffering來是因爲這樣的:

require('image').get.signUpConn(data, function(scrollView){ 
    imag.setImage(scrollView); // Why do you need this here? 
}); 

它必須是此相反,沒有必要在get

require('image').signUpConn(data, function(scrollView){ 
    imag.setImage(scrollView); 
}); 

但是,這是一個更深層次的問題,您調用callback太早,在image.js甚至加載圖像之前。另外,你爲什麼要創建scrollView(這只是一個常規視圖)並通過回調傳遞它?爲什麼你有一個循環只能循環一次?

它看起來像部分地解決這個問題,移動滾動視圖之外的這一切,通過images陣列回調,然後使用回調像這裏面的廣場功能添加圖片:

require('image').signUpConn(data, function(images){ 
    for(var i = 0;i<images.length;i++) {  
     scrollView.add(square(image[i]); 
    } 
}); 

你將不得不改變你的方形功能中的一些東西。

+0

好吧.. 正如我剛纔所說,我真的很糟糕,來回傳遞,所以如果你能幫助我,也許我會明白它是如何工作的。 這裏是我的tableView的完整代碼。 http://pastie.org/5676117 這裏是我的image.js http://pastie.org/5676123 感謝名單 –

+0

嗨。我已經縮小了我的image.js到這個:http://pastie.org/5678184 –

+0

優化你的問題是更具體的,我不知道你問我什麼。你有沒有在網上學習過關於[回調](http://recurial.com/programming/understanding-callback-functions-in-javascript/)和[一般JavaScript]的文獻(https://developer.mozilla.org/en-美國/文檔/ JavaScript的/ A_re-introduction_to_JavaScript)? –