2017-04-18 36 views
0

我目前正在學習Electron並使用AngularJS。 我在節點和表達式中使用了REST api,而我的數據庫是MongoDB。角度在電子,爲大json呈現緩慢

這是我的路線

router.get('/branch/:branch',function (req,res,next) { 
var br = req.params.branch; 
var final = []; 
    db.collection('users').find({"branch": br}, function (err, docs) { 
     docs.forEach(function (ele) { 
      console.log(ele['fir_id']); 
      final.push(ele['fir_id']); 
     }) 
     db.punch_coll.find(function (err, docs) { 
      var d = []; 
      console.log(final); 
      docs.forEach(function (ele) { 
       console.log(ele['fir_id']); 
       if(final.includes(parseInt(ele['fir_id']))) { 
        ele['date'] = ele['date'].toDateString(); 
        ele['punch_in'] = ele['punch_in'].substring(0, 8); 
        ele['punch_out'] = ele['punch_out'].substring(0, 8); 
        d.push(ele); 
       } 
       console.log(d); 
      }) 
      console.log(d); 
      res.send(d); 
     }) 
    }); 
}); 

punch_coll文件

{ 
    _id: 58e21075e0c6800ce8b08d92, 
    fir_id: '4', 
    date: 'Mon Apr 03 2017', 
    punch_in: '14:35:57', 
    punch_out: '' 
} 

用戶文檔

{ 
    _id: 58e20ee0e0c6800ce8b08d82, 
    name: 'A B C', 
    fir_id: 1, 
    branch: 'CSE', 
    year: 'SE' 
} 

HTML和角度控制腳本

<body ng-app="myApp" ng-controller="myCtrl"> 
    <form class="pure-form"> 
    <strong>Enter FIR-ID</strong> <input type="text" ng-model="fid" ng- 
change="change()" class="pure-input-rounded"> 
    </form> 
    </br> 
    <div class="pure-g"> 
    <div class="pure-u-8-24" style="border-style: solid;border- 
    color:lightgrey;"> 
    <header class="w3-container w3-light-grey"> 
     <h2>Fir ID :- {{fid}}</h2> 
     <h3>Name :- {{user[0].name}} </h3> 
    </header> 
    <div class="w3-container"> 
    <h2>Branch :- {{user[0].branch}} </h2> 
    <hr> 
    <h2>Academic Year :- {{user[0].year}} </h2> 
    </div> 
</div> 
</div> 
<form class="pure-form"> 
    <select id="state" ng-model="branch" ng-change="changeBranch()"> 
     <option>CSE</option> 
     <option>MECH</option> 
     <option>CIVIL</option> 
     <option>ENTC</option> 
    </select> 
</form> 


<!-- <h2>Fir ID :- {{fid}}</h2> 
<h2>Name :- {{user[0].name}} </h2> 
<h2>Branch :- {{user[0].branch}} </h2> 
<h2>Academic Year :- {{user[0].year}} </h2> --> 
<div style="right:0;top:0;position:absolute;font-size:20px;"> 
    <table class="pure-table pure-table-horizontal"> 
    <thead> 
    <tr> 
     <th>Fir ID</th> 
     <th>Date</th> 
     <th>Punch In</th> 
     <th>Punch Out</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr ng-repeat="x in name"> 
     <td>{{ x.fir_id }}</td> 
     <td>{{ x.date }}</td> 
     <td>{{ x.punch_in }}</td> 
     <td>{{ x.punch_out }}</td> 
    </tr> 
    </tbody> 
</table> 
</div> 
    </body> 

    <script> 
    // You can also require other files to run in this process 
    var app = angular.module('myApp', []); 
    app.controller('myCtrl', function($scope,$http) { 
    $scope.name; 
    $scope.user; 
    $scope.fid; 
    $scope.branch='CSE'; 
    $scope.change = function() { 
    //get punches for specific fir_id 
    $http.get('http://localhost:3000/users/'+$scope.fid) 
     .then(function(response) { 
      $scope.user=response.data; 
     }) 

    //get punches for specific fir_id 
    $http.get('http://localhost:3000/punch/'+$scope.fid) 
     .then(function(response) { 
      console.log(response.status); 
      $scope.name=response.data; 
     }, function errorCallback(response) { 
      $scope.name=null; 
     }); 
    }; 
    $scope.changeBranch = function(){ 
    //get record as per branch 
    $http.get('http://localhost:3000/branch/'+$scope.branch) 
     .then(function(response) { 
      console.log(response.status); 
      $scope.name=response.data; 
     }, function errorCallback(response) { 
      $scope.name=null; 
     }); 
    }; 
}); 

表格渲染緩慢的大json需要1秒,它就像它滯後。 我是新來的,所以我當然在做一些可怕的事情。我認爲我使用異步函數的方式也很糟糕,但不知道是什麼讓它慢到foreach或其他任何東西。

+0

你有多少元素循環使用? – Borjante

+0

@Borjante 80+ elements –

回答

0

所以,用簡單的for循環替換foreach後,它解決了這個問題,但我不知道確切的原因是什麼。任何人有同樣的問題?

+0

http://thejsguy.com/2016/07/30/javascript-for-loop-vs-array-foreach.html 這裏是一篇關於兩個循環之間差異的文章,for loop loop通過信息更快,因爲它是通過索引而不是按項目來完成的。 -Cheers – DDelgro