承诺使用 PouchDB 和 AngularJs ng-repeat 进行排列
Promise to array with PouchDB and AngularJs ng-repeat
我正在将 PouchDB 数据库中的 allDocs() 读取到 AngularJS 变量中:
var db = pouchService.db;
$scope.recordlist = db.allDocs({startkey: 'move_', endkey: 'move_\uffff', include_docs: true});
console.log($scope.recordlist);
我注意到它 returns 是一个承诺,当我尝试使用 ng-repeat 读取数组(以及数组内对象的属性)时,它实际上无法访问结果,我猜是因为它们嵌套很深。
<div class="row msf-row"
ng-repeat="record in recordlist | filter: shouldShow"
ng-class="{ 'msf-cancelled': record.cancelled, 'msf-commented' : record.comment}">
<div class="col-md-1">{{record.time}}</div>
</div>
有什么方法可以将这个 promise 变成一个简单的对象数组吗?
我的应用里也加载了LoDash,不知道能不能用
您正在做的是访问承诺,而不是承诺结果。虽然 allDocs
确实 return 承诺,但它不是 angular 承诺,因此您还应该将承诺包装在 when
中以获得实际的 angular承诺。
var pouchPromise = db.allDocs({startkey: 'move_', endkey: 'move_\uffff', include_docs: true});
$q.when(pouchPromise).then(function(recordList){
$scope.recordList = recordList;
console.log($scope.recordlist);
});
我会仔细阅读 promise 的工作原理 here。
需要注意的是,实际的 pouchDB 文档中概述了这种使用 pouch 的方法:http://pouchdb.com/api.html
具体来说:
Using Ionic/Angular? You can wrap PouchDB promises in $q.when(). This will notify Angular to update the UI when the PouchDB promise has resolved.
这将允许您在处理非 angular promises.
的异步性质时避免使用 $scope.$apply()
在承诺完成后分配数组(或如果发生错误则显示错误):
$q.when(db.allDocs({startkey: 'move_', endkey: 'move_\uffff', include_docs: true}))
.then(function (recordlist) {
$scope.recordList = recordList;
console.log($scope.recordlist);
})
.catch(function (error) {
console.error(error);
});
更新:
正如 Matt 在评论中指出的那样,如果您不使用 angular-pouch angular-pouchdb 包装器,那么您需要将操作包装在 $scope.$apply()
中以触发摘要循环或首先转换angular 与 $q.when()
的承诺。我认为将承诺转换为 angular 承诺也会处理记录错误,但您应该始终处理错误(向用户显示消息)。您当然可以使用全局错误处理程序来执行此操作。
我正在将 PouchDB 数据库中的 allDocs() 读取到 AngularJS 变量中:
var db = pouchService.db;
$scope.recordlist = db.allDocs({startkey: 'move_', endkey: 'move_\uffff', include_docs: true});
console.log($scope.recordlist);
我注意到它 returns 是一个承诺,当我尝试使用 ng-repeat 读取数组(以及数组内对象的属性)时,它实际上无法访问结果,我猜是因为它们嵌套很深。
<div class="row msf-row"
ng-repeat="record in recordlist | filter: shouldShow"
ng-class="{ 'msf-cancelled': record.cancelled, 'msf-commented' : record.comment}">
<div class="col-md-1">{{record.time}}</div>
</div>
有什么方法可以将这个 promise 变成一个简单的对象数组吗?
我的应用里也加载了LoDash,不知道能不能用
您正在做的是访问承诺,而不是承诺结果。虽然 allDocs
确实 return 承诺,但它不是 angular 承诺,因此您还应该将承诺包装在 when
中以获得实际的 angular承诺。
var pouchPromise = db.allDocs({startkey: 'move_', endkey: 'move_\uffff', include_docs: true});
$q.when(pouchPromise).then(function(recordList){
$scope.recordList = recordList;
console.log($scope.recordlist);
});
我会仔细阅读 promise 的工作原理 here。
需要注意的是,实际的 pouchDB 文档中概述了这种使用 pouch 的方法:http://pouchdb.com/api.html
具体来说:
Using Ionic/Angular? You can wrap PouchDB promises in $q.when(). This will notify Angular to update the UI when the PouchDB promise has resolved.
这将允许您在处理非 angular promises.
的异步性质时避免使用$scope.$apply()
在承诺完成后分配数组(或如果发生错误则显示错误):
$q.when(db.allDocs({startkey: 'move_', endkey: 'move_\uffff', include_docs: true}))
.then(function (recordlist) {
$scope.recordList = recordList;
console.log($scope.recordlist);
})
.catch(function (error) {
console.error(error);
});
更新:
正如 Matt 在评论中指出的那样,如果您不使用 angular-pouch angular-pouchdb 包装器,那么您需要将操作包装在 $scope.$apply()
中以触发摘要循环或首先转换angular 与 $q.when()
的承诺。我认为将承诺转换为 angular 承诺也会处理记录错误,但您应该始终处理错误(向用户显示消息)。您当然可以使用全局错误处理程序来执行此操作。