【问题标题】:Reopening PouchDB in a second Angular factory results in Pouch not using SQLite plugin在第二个 Angular 工厂中重新打开 PouchDB 会导致 Pouch 不使用 SQLite 插件
【发布时间】:2016-04-04 04:45:47
【问题描述】:

我正在编写一个使用 PouchDB 和 SQLite 插件的 Ionic 应用程序。我有一家工厂负责打开数据库并公开一些获取记录的方法等:

.factory('PouchdbFactory', ['$q', function ($q) {

        var db,
            codes = [];

        var initDB = function() {
            db = new PouchDB('codes', {adapter: 'websql', auto_compaction: true, location: 2}); // location should be 2
            // Listen for changes on the database
            db.changes({ live: true, since: 'now', include_docs: true}).on('change', onDatabaseChange);

            db.info().then(console.log.bind(console));
        }
...etc...

还有另一个工厂,它返回用于定期获取记录并更改它们的方法。

 .factory('OtpFactory', ['$rootScope', '$interval', 'PouchdbFactory',  function($rootScope, $interval, PouchdbFactory) {

        var otpCodes,
            globalTimerPromise,
            totpObj = new TOTP();

        // this breaks sqllite plugin (db.info().then(console.log.bind(console)); returns sqlite_plugin: false )
        PouchdbFactory.initDB();

        PouchdbFactory.getAllCodes().then(function(codes) {
            otpCodes = codes;
        });

在第二个工厂调用 PouchdbFactory.initDB(); 会导致 pouch 不使用 SQLite 插件(db.info().then(console.log.bind(console)); 返回 sqlite_plugin: false)。

我还在学习 Angular,所以也许这不是构建它的方式。我可以将数据库创建移动到$rootscope,这样所有工厂都可以访问数据库,但我不明白为什么第二次调用PouchdbFactory.initDB();意味着没有sqlite_plugin。

有什么想法吗?

【问题讨论】:

    标签: angularjs ionic-framework pouchdb


    【解决方案1】:

    这不是错误的原因,但如果数据库实例已初始化,我宁愿只返回它。尝试在 init 中检查 db 是否已初始化的保护子句 - 如果它立即返回 db,如果没有则继续初始化它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多