2015-02-23 45 views
0

我试着创建一个配置文件来保留我的应用程序的一些配置。我正在使用SAPUI5和cordova文件。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**}; 

} 

感谢您的帮助!

+0

您是否可以在捆绑应用程序时将这些值存储在.json文件中?就值的变化而言,您可以将新值存储在本地存储中。这个策略似乎比担心在文本文件中保存文件系统的值简单得多。 – njtman 2015-02-23 17:43:04

+0

谢谢njtman!很有帮助 – mayconbelfort 2015-02-24 23:26:07

回答

0

正如Njtman所建议的那样,我只是使用localstorage将信息保存在没有cordova文件插件的文件中。

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

的index.html上deviceready事件:

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文件。