【发布时间】: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