Angularjs函数性能导致数据加载问题。

时间:2017-07-12 14:27:33

标签: angularjs function loading array.prototype.map

这是我用来从后端检索报告的函数,然后通过映射过程将来自另一个数组的键映射到对象中的每个数组的键。 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;
  }

1 个答案:

答案 0 :(得分:0)

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

 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;
            });
          }