2015-02-23 34 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文件。