AngularJS Return IndexedDB 调用后的方法对象

AngularJS Return object of methods after IndexedDB call

像这样调用 IndexedDB 库后,我需要 return 一个方法对象。

.factory('presentationData', ['$indexedDB', 'uuid4','$q', function ($indexedDB, uuid4, $q) {

  var objectStore = $indexedDB.openStore('presentations', function(store) {
    return store;
  });

  return objectStore.then(function(objectStore) {
    var functions = getDefaultDataServiceFunctions(objectStore);

    functions.getAll = function (includeFlaggedForDelete) {
      if (includeFlaggedForDelete) {
        return objectStore.getAll();
      }
      return objectStore.getAll().then(function (presentations) {
        return presentations.filter(function (p) {
          return !(p.meta && p.meta.localStatus === 'deleted');
        });
      });
    };

    functions.getWithLocalStatus = function (status) {
      var query = $indexedDB.queryBuilder().$index('localStatus').$eq(status).compile();
      return objectStore.each(query);
    };

    return functions;
  })
}])

为什么这不是 return 方法对象?没看懂!!

使用这个:https://github.com/bramski/angular-indexedDB

谢谢!

这是行不通的,因为 return存储对象无效。商店将关闭,对其的操作将失败。

我真的不清楚你到底想做什么。只拿数据?您需要 return 一个将 return 数据的承诺。

.service('dataFetcher', ['$indexedDB', 'uuid4','$q', function ($indexedDB, uuid4, $q) {
  this.getData = function() {
    promise = $q.defer();
    $indexedDB.openStore('presentations', function (store) {
      ... do what is needed to get the answer here...
      promise.resolve(data);
    }
    return promise.promise;
  }
}

然后使用此服务就像...

.controller('MyControl', ['dataFetcher', function ($scope, dataFetcher) {
  dataFetcher.getData().then( function (data) {
    $scope.data = data;
  });
}];

这会给你你真正想要的。 indexed-DB 采用回调机制。您需要使用它以合理的方式设计您的数据服务。