我使用ForerunnerDB -一个NoSQL JSON文档DB在客户端(web)上创建数据存储。这个特定的库提供了一个持久化存储选项。然而,它似乎有什么问题。我在下面列出了我的代码:
应用程序:
(function(){
angular.module("TestApp", ['ui.router', 'LocalStorageModule', 'forerunnerdb']);})();
控制器:
(function(){
angular.module("TestApp")
.controller("FetchGroupsController", function(clientStore, $scope, $http){
clientStore.getTable("groups").load(function (err, tableStats, metaStats) {
if (err) {
// Load operation was unsuccessful
console.error("error in pulling collection " + name);
console.error(err)
}
else{
//there is persisted data corresponding to the requested table
if(tableStats.foundData && tableStats.rowCount > 0){
controller.groups = clientStore.fetchRecords("groups"); //returns an array of objects
console.log("User has " + tableStats.rowCount + " groups");
console.log(controller.groups);
}
else{//no table for group data persisted */
$http({ method: "POST", url: "api/groups/index"})
.success(function(data, status){
//TODO: handle the activation code
if(status == 200){
delete data["debug"]; //deleting an unnecessary object from response data
clientStore.addList("groups", data);
controller.groups = clientStore.fetchRecords("groups");
console.log(controller.groups);
clientStore.getTable("groups").save();
}
else if(status == 403){ //forbidden
controller.messages = data;
}
else if(status == 401){ //error
controller.errors = data;
}
})
;
}
}
});
});
})();服务:
angular.module("TestApp")
.factory('clientStore', function($fdb, $http, localStorageService){
var clientDB = null;
var factory = {};
clientDB = $fdb.db("testDB");
factory.getDB = function(){
return clientDB;
};
factory.createTable = function(name, pk){
if(pk != false){
return clientDB.collection(name, {primaryKey : pk});
}
return clientDB.collection(name);
};
factory.resetTable = function(name){
clientDB.collection(name)._data = [];
clientDB.collection(name).save();
};
factory.getTable = function(name){
return clientDB.collection(name);
};
factory.add = function(name, record){ //insert a single object
clientDB.collection(name).insert(record/*, function(result){
console.warn(result);
}*/);
};
factory.addList = function(name, records){ //insert a list of objects
for (record in records){
clientDB.collection(name).insert(records[record]);
}
};
factory.fetchRecords = function(name){
return clientDB.collection(name)._data;
};
return factory;
});视图:
<div ng-repeat="group in fetchGrpsCtrl.groups" class="row">
<div class="col-md-12">
<div class="groups" id="grp-@{{group.id}}" ng-click="fetchGrpsCtrl.setGroup(group)">
<div class="details">
<h5> @{{group.title}}</h5>
</div>
</div>
</div>
</div>问题是变量controller.group在视图中为null,但是在控制器中,当我将它(第15行)登录到控制台时,我将得到它的期望值。我认为这可能是一个范围问题,回调中对变量所做的更改没有在视图中得到。但情况并非如此,因为我注释掉了行clientStore.getTable(“group”).save();然后再次运行它,视图按预期完美地更新了。我不明白为什么save()函数正在擦除视图中的controller.groups变量,但是在控制器中完全注销了。
发布于 2016-07-09 16:31:15
您使用ForerunnerDB不正确。它包括一个AngularJS插件,这使得在AngularJS中使用它非常容易,但是您已经编写了一个自己的包装器,它连接到集合的_data属性上(这是不应该做的,因为该属性是由ForerunnerDB内部管理的,并且可以被销毁和重新创建,这意味着您对可能不再被集合使用的对象的引用不正确)。
相反,您应该在AngularJS中使用AngularJS,如文档:https://github.com/Irrelon/ForerunnerDB#angularjs-and-ionic-support中所述
ForerunnerDB包括一个名为"ng()“的助手方法,它允许您正确地将数据从集合链接到视图的作用域。
另外,在使用第三方javascript库时,带有下划线的属性通常被认为是“私有的”,不应该直接访问。您应该使用访问器方法(例如,在ForerunnerDB的情况下是.find() )来访问数据。通常,以下划线开头的属性很好地表明它是私有的(不是所有的库和程序员都遵循这一约定,但很多人遵循这种约定)。
https://stackoverflow.com/questions/38283766
复制相似问题