2016-09-20 66 views
0

這麼快,我難住了。通過Appcelerator將值正確傳遞給一個函數

尋找在我的Appcelerator應用程序中顯示一個彈出窗口,它工作正常,但我似乎無法獲得並將變量傳遞給我的函數,因爲它不斷返回未定義。

彈出式代碼片段不在這裏發佈,因爲這不是問題。

這裏可能是一個完整的小學生錯誤,任何人都能指出我做錯了什麼?

因此,當用戶點擊PosterView(通過HTTP請求即時創建)時,我需要將「ourscreenName」和「ourscreenURL」傳遞到showOurscreenBooking函數中。

var xhr = Ti.Network.createHTTPClient({ 
      onload: function() { 
       // Ti.API.debug(this.responseText); 

       json = JSON.parse(this.responseText); 
       Ti.API.info(json); 
       for (i = 0; i < json.length; i++) { 

        posterView = Ti.UI.createImageView({ 
         width: Ti.Platform.displayCaps.platformWidth/3, 
         defaultImage: 'images/imgloader.png', 
         top: 0, 
         backgroundColor: '#000' 
        }); 

        getPoster(json[i].tmdb_id, posterView, json[i].ourscreen_film_name); 

        ourscreenName = json[i].ourscreen_film_name; 
        ourscreenURL = json[i].ourscreen_url; 

        scrollView.add(posterView); 

        posterView.addEventListener('click', function(e) { 
         // need to open up the booking popup 
         showOurscreenBooking(e.ourscreenName, e.ourscreenURL); 
        }); 

       } 

      }, 
      onerror: function(e) { 
       Ti.API.debug("STATUS: " + this.status); 
       Ti.API.debug("TEXT: " + this.responseText); 
       Ti.API.debug("ERROR: " + e.error); 
       alert('There was an error retrieving the remote data in event loop. Try again.'); 


      }, 
      timeout: 30000 // add longer timeout for slower connections 
     }); 

謝謝!

西蒙

回答

0

不要認爲這是一個最好的做法(我真的需要一個較長的工作一天後,鏡頭的新鮮空氣),但你可以做的是:

getPoster(json[i].tmdb_id, posterView, json[i].ourscreen_film_name); 

ourscreenName = json[i].ourscreen_film_name; 
ourscreenURL = json[i].ourscreen_url; 

// add your properties as custom properties to the view 
posterView = Ti.UI.createImageView({ 
    width: Ti.Platform.displayCaps.platformWidth/3, 
    defaultImage: 'images/imgloader.png', 
    top: 0, 
    backgroundColor: '#000', 
    ourscreenName: ourscreenName, 
    ourscreenUrl: ourscreenUrl 
}); 

scrollView.add(posterView); 

posterView.addEventListener('click', function(e) { 
    showOurscreenBooking(e.source.ourscreenName, e.source.ourscreenURL); 
}); 

另見https://archive.appcelerator.com/question/12111/event-propagation----how-to-use-eventsource-property

+0

就是這樣!我只是有一個小問題,如果你點擊第一張海報,你會得到正常的行爲。如果你點擊第二張海報,你會得到第一張海報信息,然後是第二張海報。如果你點擊第三個,你就會得到第一,第二和第三張海報信息。 –

+0

我的第一個想法是:讓它'var posterView = Ti.UI.createImageView ...',而不僅僅是posterView,你可能已經把posterView定義爲全局的,並且讓我知道它是否可以解決你的問題。 – davidcyp

相關問題