【问题标题】:Angularjs function performance causing data loading issues.Angularjs 函数性能导致数据加载问题。
【发布时间】:2017-12-17 01:09:38
【问题描述】:

这是我用来从后端检索报告的函数,然后通过映射过程将键从另一个数组映射到对象中每个数组分组的键。 getReport() 函数最初从后端请求数据,但数据在第一次映射后返回为空。一旦再次请求数据,一切都会完美运行。

  function getReport() {
    vm.loading = true;
    vm.selectedReport.reportFunc(location._id, vm.beginDate, vm.endDate, vm.orderStatus.value)
      .then(report => {
        switch(vm.selectedReport.name) {
          case 'Sales by Section': 
            getSectionIds();
            vm.report = prepareSectionsReport(report);
            vm.mergedSectionReport = prepareLineItemSales(vm.report).filter( (line) => { 
              if (line) {
                return line;
              }
            });
            break;
          default:
            vm.report = report;
            break;
        }
        vm.loading = false;
      });
  }

function prepareSectionsReport(report) {
    var r = Object.keys(report).map( (sectionId) => {
      for (var section of sections) {
        if (section && section.sectionId === sectionId) {
          let total = report[sectionId].slice(-1)[0];
          return {
            sectionName: section.name,
            section: report[sectionId],
            total: total
          };
        } else if (sectionId === 'N/A') {
          let total = report[sectionId].slice(-1)[0];
          return {
            sectionName: 'N/A',
            section: report[sectionId],
            total: total
          }
        }
      }
    });

    r = removeUndefined(r);

    return r;
  }

【问题讨论】:

    标签: angularjs function loading array.prototype.map


    【解决方案1】:

    问题是由函数加载的顺序引起的,并且使用 Promise 解决了。

     function getReport() {
                vm.loading = true;
                vm.selectedReport.reportFunc(location._id, vm.beginDate, vm.endDate, vm.orderStatus.value)
                  .then(report => {
                    switch(vm.selectedReport.name) {
                      case 'Sales by Section': 
                        prepareSectionsReport(report).then( (r) => {
                          vm.report = r;
                          return vm.report;
                        }).then( (r) => {
                          vm.mergedSectionReport = prepareLineItemSales(vm.report).filter( (line) => { 
                            if (line) {
                              return line;
                            }
                          });
                        });
                        break;
                      default:
                        vm.report = report;
                        break;
                    }
                    vm.loading = false;
                  });
              }
    
              function getSectionIds() {
                return MenuService.query({ location: location._id }).$promise
               }
    
              function prepareSectionsReport(report) {
                return getSectionIds().then( (menus) => {
                  menus[0].sections.forEach( (section) => {
                    vm.sections.push( {
                      sectionId: section._id,
                      name: section.name
                    });
                  });
                }).then( () => {
    
                  console.time('sections');
                  var r = Object.keys(report).map( (sectionId) => {
                    /* On initial load does not execute this section properly on execute */
                    for (var section of vm.sections) {
                      if (section && section.sectionId === sectionId) {
                        let total = report[sectionId].slice(-1)[0];
                        return {
                          sectionName: section.name,
                          section: report[sectionId],
                          total: total
                        };
                      } else if (sectionId === 'N/A') {
                        let total = report[sectionId].slice(-1)[0];
                        return {
                          sectionName: 'N/A',
                          section: report[sectionId],
                          total: total
                        }
                      }
                      /* end of section*/
                    }
                  });
                  console.timeEnd('sections');
    
                  r = removeUndefined(r);
                  return r;
                });
              }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 2021-03-16
      • 2015-12-22
      • 1970-01-01
      • 2019-05-04
      相关资源
      最近更新 更多