【问题标题】:SAPUI5 using cordova file to create config fileSAPUI5使用cordova文件创建配置文件
【发布时间】: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!很有帮助

标签: android cordova sapui5


【解决方案1】:

按照 Njtman 的建议,我必须使用 localstorage 将信息保存在没有科尔多瓦文件插件的文件中。

我想分享找到的解决方案。

deviceready 事件中的 index.html:

jQuery.sap.require("model.Config");

var conf = new Configuration();

sap.ui.getCore().setModel(conf, "Config");

conf.init();

配置类:

sap.ui.model.json.JSONModel.extend("Configuration", {

url: "",
port: "80",
defaultport: true,
ldap: false,

init : function() {
    var deferred = $.Deferred();
    console.log("INITIALIZING...");

    var config = JSON.parse(window.localStorage.getItem("config"));

    if(config == null){
        console.log("CONFIG IS NULL");
        window.localStorage.setItem("config", JSON.stringify(
                {"URL": this.url, "PORT": this.port, "DEFAULTPORT": this.defaultport, "LDAP": this.ldap}
            ));         
    }

    deferred.resolve();
    this.setData(JSON.parse(window.localStorage.getItem("config")));
    this.setVars();

    console.log(this.getJSON());

    return deferred.promise();
},

save: function(url, port, defaultport, ldap){

    var deferred = $.Deferred();
    console.log("SAVING...");

    window.localStorage.setItem("config", JSON.stringify(
           {"URL": url, "PORT": port, "DEFAULTPORT": defaultport, "LDAP": ldap}
        ));

    deferred.resolve();
    this.setData(JSON.parse(window.localStorage.getItem("config")));

    this.setVars();

    return deferred.promise();

},

setVars: function(){
    this.url = this.getProperty("/URL");
    this.port = this.getProperty("/PORT");
    this.defaultport = this.getProperty("/DEFAULTPORT");
    this.ldap = this.getProperty("/LDAP");

}
});

现在我可以读取和更新我的 json 文件了。

【讨论】:

    猜你喜欢
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 2015-06-24
    • 2012-05-09
    • 2011-08-09
    • 2020-05-17
    相关资源
    最近更新 更多