I have a problem with variable scope in WinJS. When the variable is changed it should be visible in bigger scope, but after call function this variable has value only inside function. I think it is problem with readTextAsync, because when I fill variable in function without readTextAsync, it is working.
This is variable declaration:
var fileDate;
This is function where I call another:
WinJS.UI.Pages.define("index.html", {
ready: function (element, options) {
loadDate();
console.log("główna " + fileDate); //fileDate = undefined
this.fillYearSelect();
},
And this is function, where variable is changed:
localFolder.getFileAsync(filename).then(function (file) {
Windows.Storage.FileIO.readTextAsync(file).done(function (fileContent) {
fileDate = fileContent; // example - fileDate=a073z160415
console.log("fileDate " + fileDate);
},
function (error) {
console.log("Reading error");
});
},
function (error) {
console.log("File not found");
});
}
P.S. Sorry for my English. It isn't perfect :)
I think it is problem with readTextAsync, because when I fill variable in function without readTextAsync, it is working.
I made this answer from your last post's codes.Windows.Storage.FileIO.readTextAsync
is a windows async api. So it should be handled in async way: console.log("główna " + fileDate)
should be handled in loadDate().then()
like below and fileContent
should be returned,and you can catch it in loadDate().then(function(data){})
.
WinJS.UI.Pages.define("index.html", {
ready: function (element, options) {
loadDate().then(function(data){
fileDate=data; //here catch the fileContent data
console.log("główna " + fileDate);
});
this.fillYearSelect();
},
function loadDate() {
var that = this;
var filename = "abc.txt";
return Windows.Storage.ApplicationData.current.localFolder.getFileAsync(filename).then(function (file) {
return Windows.Storage.FileIO.readTextAsync(file).then(function (fileContent) {
return fileContent;//here return the fileContent.You can catch it outside.
},
function (error) {
console.log("Błąd odczytu");
});
},
function (error) {
console.log("Nie znaleziono pliku");
});
}