数据库查询与更新AngularJS中的$ scope变量& IndexedDB的

时间:2016-10-01 17:46:12

标签: javascript angularjs indexeddb

我在AngularJS& amp;运行的离线应用中有3个步骤。索引资料。

在页面加载时,我进行数据库调用以列出具有今天日期的所有记录(转换为recordlist变量):

$indexedDB.openStore('records', function(store){
  $scope.todaystart = moment().startOf('day').format(); 
  $scope.todayend = moment().endOf('day').format(); 
  var find = store.query();
    find = find.$between($scope.todaystart, $scope.todayend, false, false);
    find = find.$index("date_idx");
    store.eachWhere(find).then(function(e){
        $scope.recordlist = e;
    });
});

然后我可以添加新记录,但为了更新recordlist,我还在另一个电话

$scope.addRecord = function (records) {
  $indexedDB.openStore('records', function(store){
    $scope.newrecord = {
      "date": $scope.dateJson, 
      "time": $scope.time, 
      "arrival": '',
      "inserted": ''
    }
    store.insert($scope.newrecord).then(function(e){});
      var find = store.query();
      find = find.$eq($scope.dateJson);
      find = find.$index("date_idx");
      store.eachWhere(find).then(function(e){
        $scope.recordlist = e;
      });
  });
};

最后我用到达时间更新记录,进行又一次数据库调用

$scope.insertRecord = function (record) {
  $indexedDB.openStore('records', function(store){
    store.upsert(
    { 
      "ssn": record.ssn,
      "date": record.date,
      "time": record.time, 
      "arrival":  moment().format("HH.mm.ss"),
      "inserted": true,
    }).then(function (e) {
      var find = store.query();
      find = find.$eq($scope.dateJson);
      find = find.$index("date_idx");
      store.eachWhere(find).then(function(e){
          $scope.recordlist = e;
      });
    });
  });
};

我的问题是,考虑到我已经在第一次调用时检索recordlist,在不必进行服务器查询的情况下即时更新也不会更清晰 - 更好每一步(添加/插入)?

最后,无论如何都会保存数据,当我可能只是将新数据附加到recordlist时,是否需要一遍又一遍地检索它带来的性能问题?

如果是这样,我该怎么做?

我是使用数据库的新手,在最佳实践和工作流程方面我有点迷失......

0 个答案:

没有答案