【问题标题】:Persistence storage in ForerunnerDB not working properlyForerunnerDB 中的持久性存储无法正常工作
【发布时间】:2016-07-09 16:12:52
【问题描述】:

我正在使用ForerunnerDB - a NoSQL JSON Document DB 在客户端(网络)上创建数据存储。这个特定的库提供了一个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 在视图中为空,但在控制器中,当我将它(第 15 行)记录到控制台时,我得到了预期值。我认为这可能是范围问题,并且在回调中对变量所做的更改没有在视图中被拾取。但事实并非如此,因为我注释掉了这一行 clientStore.getTable("groups").save(); 并再次运行它,视图按预期完美更新。我无法弄清楚为什么 save() 函数正在擦除视图中的 controller.groups 变量,但在控制器中却完美地注销了。

【问题讨论】:

    标签: angularjs datastore persistent-storage forerunnerdb


    【解决方案1】:

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

    您应该按照文档中的说明在 AngularJS 中使用 ForerunnerDB:https://github.com/Irrelon/ForerunnerDB#angularjs-and-ionic-support

    ForerunnerDB 包含一个名为“ng()”的辅助方法,允许您将数据从集合正确链接到视图的范围。

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

    【讨论】:

    • 我实际上正在使用角度插件。我还将添加应用程序代码以消除混乱。我创建了该服务,以便为数据库提供一个流畅的界面,我可以在应用程序方面进行操作(使用 $rootScope 感觉不舒服)。我错过了 ng() 辅助方法。谢谢你指出这一点。现在工作顺利。我也删除了 fetchRecords() 函数。干杯!
    • @SpiderWasp42 没问题,伙计! :)
    猜你喜欢
    • 2016-07-16
    • 1970-01-01
    • 2013-09-23
    • 2013-11-13
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多