【发布时间】:2015-02-25 07:34:03
【问题描述】:
我正在尝试创建一个配置文件来保留我的应用程序的一些配置。我正在使用 SAPUI5 和 cordova 文件。
目的是创建一个 conf.txt 来保存 URL、PORT 和 LDAP 数据以访问我的系统。但是,这些信息可能会发生变化,因此我需要更新文件。
在我的应用程序中,我已经在应用程序启动时使功能 deviceready,并创建了 conf.txt:
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
/*jQuery.sap.require("model.Config");
var conf = new Configuration();
conf.init();*/
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("conf.txt", {create : true,exclusive : false},gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
//alert(fileEntry.fullPath);
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
alert("OK");
};
var conf = "URL=\r\nPORT=80\r\nLDAP=false";
writer.seek(writer.length);
writer.write(conf);
}
function fail(error) {
alert(error.code);
}
我没有做任何与其他示例不同的事情。但是,正如我在onDeviceReady 函数中评论的那样,我尝试创建一个类来创建、读取和更新文件。
我发现的所有示例都引用了 deviceready 事件。我可以在这个事件上只使用 FileWriter 和 FileReader 的方法吗?
这是我的配置类:
function Configuration() {
this.fileName = "conf.txt";
this.init = function() {**How to use the cordova API here**};
this.read = function(){**How to use the cordova API here**};
this.update= function(){**How to use the cordova API here**};
}
感谢您的帮助!
【问题讨论】:
-
捆绑应用时能否将这些值存储在 .json 文件中?至于值的变化,您可以将新值保存在本地存储中。这种策略似乎比担心将值持久化到文本文件中的文件系统要简单得多。
-
感谢 njtman!很有帮助