【发布时间】:2015-04-21 12:47:48
【问题描述】:
在 AngularJS 中使用 IndexedDB 时,我遇到了这个难题,如标题中所述,并且不确定什么是正确的解决方法。
在许多教程中,数据库只打开一次,仅此而已,所有方法都使用一个连接。
1234563控制器。
-
我知道保持控制器纤细是一种很好的做法,可以让它们更易于重复使用。因此,我对每个 CRUD 方法都有单独的控制器,这意味着我需要单独调用来为每个请求打开数据库,如下所示:
websiteService.openDatabase().then(function() { websiteService.addWebsite($scope.website.url, $scope.website.color).then(function() { }, function(err) { console.log(err); }); });
所以,每次我想addWebsite,我都会先openDatabase,然后等待promise解决。
websiteService 是一个工厂,所有使用 IndexedDB 的方法都在这里
这里的最佳做法是什么?如果还有其他方法,这里没有提到,请务必提及。
如果有帮助,这里是完整的控制器代码:
app.controller('AddWebsiteCtrl', ['$scope', '$location', '$routeParams', 'websiteService', function($scope, $location, $routeParams, websiteService) {
$scope.save = function() {
websiteService.openDatabase().then(function() {
websiteService.addWebsite($scope.website.url, $scope.website.color).then(function() {
}, function(err) {
console.log(err);
});
});
$location.path('/overview');
};
$scope.cancel = function() {
$location.path('/overview');
};
}]);
【问题讨论】:
标签: javascript angularjs indexeddb