我试图使用LokiJS在Ionic应用程序中持久保存数据到手机。为此,我使用了几个JS库,我们也使用了cordova-file-plugin。但是,似乎回调方法(ionic.Platform.ready())永远不会触发,并且插件永远不会被加载。知道这里发生了什么吗?
以下是我的id date date2
2 30-11-07 2007-11-30
3 17-12-07 2007-12-17
3 12-12-08 2008-12-12
文件中的工厂代码,用于处理使用LokiJS保存和加载数据:
app.js
这是我的index.html:
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngStorage', 'lokijs'])
.factory ('StorageService', function ($localStorage) {
/**Initiate database 'savedDrawings' with collection 'drawings' and a dynamic view of the names*/
var db, drawings;
ionic.Platform.ready(function(){
// will execute when device is ready, or immediately if the device is already ready.
var adapter = new LokiCordovaFSAdapter({"prefix": "loki"});
db = new loki('loki.json', {
autosave: true,
autosaveInterval: 5000,
autoload: true,
adapter: adapter
});
drawings = db.getCollection('drawings');
if (!drawings) {
drawings = db.addCollection('drawings', {
unique: ['name']
});
}
});
/**
* Searches for a creation based on its name and returns it if it exists, prints an error if it does not
*/
var _get = function (creationName) {
//return drawings.find({ 'name': creationName });
var foo = drawings.by('name', creationName);
return foo;
};
/** TODO
* Returns all saved creations
*/
var _getAll = function () {
return drawings.data;
};
/**
* Adds a creation if one of the same name does not exist
* Returns -1 if one of the same name does exist
*/
var _add = function (creation) {
if(_get(creation.name) != null){
return -1;
}
drawings.insert({
name: creation.name,
drawingLines: creation.drawingLines,
drawingSteps: creation.drawingSteps,
notesInLine: creation.notesInLine
});
db.saveDatabase();
};
/**
* Overwrites a creation by removing and then adding
*/
var _overwrite = function(creation){
_remove(creation.name);
_add(creation);
};
/**
* Searches for a creation based on name and removes it
*/
var _remove = function (creationName) {
var drawingToRemove = _get(creationName);
drawings.remove( drawingToRemove );
};
/**
* Deletes all saved creations
*/
var _removeAll = function () {
drawings.removeDataOnly();
};
/**
* These are the function names that can be called by outside functions using StorageService.(function name)
*/
return {
getAll: _getAll,
get: _get,
add: _add,
remove: _remove,
removeAll: _removeAll,
overwrite: _overwrite
};
})