2016-09-20 69 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

相关问题