简体   繁体   English

如何从ArrowDB中显示单个对象的详细信息#appcelerator

[英]How to show details from ArrowDB for individual objects #appcelerator

I have queried from ArrowDB and gotten a list of events, now I want each individual event details when the specific view is clicked. 我已经从ArrowDB查询并获得了事件列表,现在我希望单击特定视图时每个事件的详细信息。 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
            }
         }
     });

The problem lies in that you are using a for-loop whose code block does not have its own scope. 问题在于您正在使用for循环,其代码块没有自己的作用域。 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. event.name您使用的detailstestname标签永远是最后的循环由循环已完成,当时的事件click事件被处理。

You could either use a closure: 您可以使用闭包:

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

Or use .forEach() : 或使用.forEach()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从“子级”类/表中搜索,分配和打印对象-(Parse.com + Appcelerator) - How to search, assign and print objects from a “child” Class/Table - (Parse.com + Appcelerator) 如何在不同对象中显示数组中的各个值? - How to display individual values from an array in different objects? 如何使用 Angular 从多个对象的列表中获取单个值 - How to get the individual value from a list of multiple objects using Angular 如何从一个大型JSON对象解析单个JSON对象 - how to parse individual JSON objects from one large JSON object 用户从列表中选择时如何显示内容详细信息? - How to show content details when user selects from list? 如何从 map 两个对象数据中提取细节? - How to map two objects data and extract the details from one? 如何在发出HTTP请求时显示微调框(Appcelerator) - How to show a spinner while making an HTTP request (Appcelerator) 如何从Appcelerator中的控制器传递数据 - How to pass data from controller in appcelerator 从NG绑定结果访问单个对象 - Accessing Individual objects from NG Bind Result 如何在对象数组的模板属性中显示 - How to show in template property from array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM