ForerunnerDB 中的持久性存储无法正常工作

Persistence storage in ForerunnerDB not working properly

我正在使用 ForerunnerDB - a NoSQL JSON Document DB for creating a data store on the client (web). This particular library offers a persistence storage 选项。但是,它似乎有问题。我在下面包含了我的代码:

应用程序:

(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("groups").保存(); 并 运行 再次显示,视图已按预期完美更新。我无法弄清楚为什么 save() 函数正在擦除视图中的 controller.groups 变量,但在控制器中却被完美地注销了。

您错误地使用了 ForerunnerDB。它包括一个 AngularJS 插件,这使得它与 AngularJS 一起使用非常容易,但是您已经编写了一个自己的包装器,该包装器挂接到集合的 _data 属性 上(您不应该这样做,因为 属性 由 ForerunnerDB 内部管理,可以被销毁和重新创建,这意味着您将错误地引用一个可能不再被集合使用的对象。

相反,您应该按照文档中的描述在 AngularJS 中使用 ForerunnerDB:https://github.com/Irrelon/ForerunnerDB#angularjs-and-ionic-support

ForerunnerDB 包含一个名为 "ng()" 的辅助方法,它允许您将集合中的数据正确 link 到您的视图范围。

此外,当使用第三方 javascript 库时,带下划线的属性通常被视为 "private",不应直接访问。您应该使用访问器方法(例如 ForerunnerDB 中的 .find() )来访问数据。通常以下划线开头的 属性 很好地表明它是私有的(并非所有库和程序员都遵循此约定,但很多都这样做)。