如何从 ArrowDB 显示单个对象的详细信息#appcelerator [英] How to show details from ArrowDB for individual objects #appcelerator

查看:25
本文介绍了如何从 ArrowDB 显示单个对象的详细信息#appcelerator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从 ArrowDB 查询并获得了一个事件列表,现在我想要在单击特定视图时每个单独的事件详细信息.我的代码如下所示,但它将所有查询事件的详细信息放在打开的窗口中,并在顶部显示最后查询的事件.

I have queried from ArrowDB and gotten a list of events, now I want each individual event details when the specific view is clicked. My code as shown below works except that it puts the details from all the queried events on the opened window, and it shows the last queried event at the top.

请问我该怎么做?我的大脑无法解决这个问题.

How can I do this please? My brain just can't fix this.

 Cloud.Events.query(function (e) {
        if (e.success) {
            alert('Success:\n' + 'Count: ' + e.events.length);

            for (var i = 0; i < e.events.length; i++) {
                var event = e.events[i];
                alert('id: ' + event.id + '\n' +
                      'name: ' + event.name + '\n' +
                      'start time: ' + event.start_time);
                var holdview = Ti.UI.createView({
                     backgroundColor: '#d3d3d3',
                     backgroundImage: 'testview.png',
                     width: '85%',
                     height: '85%',
                     top: '5%'
                });

                //adding holdview to a scrollable view

               xperiencesscrollableview.addView(holdview);

               var testlabeltitle = Ti.UI.createLabel({
                    top: '5%',
                    width: '65%',
                    height: '10%',
                    left: '20%',
                    text: event.name
               }); 
               //it works well to this point and shows each event separately on views
               holdview.add(testlabeltitle);
       //adding forEach as suggested as an answer            
               e.events.forEach(function(event) {
        holdview.addEventListener('click',function(e){

            var detailstestname = Ti.UI.createLabel({
        top: '5%',
        width: '65%',
        height: '10%',
        left: '20%',
        text: event.name,
        color: 'black',
        backgroundColor: 'white'
        }); 
       //Page for event details
        eventdetailspage.open();
        eventdetailspage.add(detailstestname);
        });
 });    
               //at this point it messes up
            }
         }
     });

推荐答案

问题在于您使用的 for 循环的代码块没有自己的作用域.您用于 detailstestname 标签的 event.name 将始终是在循环完成和 click 时最后一个循环的事件事件被处理.

The problem lies in that you are using a for-loop whose code block does not have its own scope. The event.name you use for the detailstestname label will always be the event of the last loop by the time the loop has finished and the click event is handled.

您可以使用闭包:

for (var i = 0; i < e.events.length; i++) {
  (function(event) {
    ..
  })(e.events[i]);
}

或者使用.forEach():

e.events.forEach(function(event) {
  ..
});

这篇关于如何从 ArrowDB 显示单个对象的详细信息#appcelerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆