locked
problem when I create file

    Question

  • hello everybody !

    I'm developing an app who creates a txt file, then write something inside, then read !

    I do this:

    default.html

    <!DOCTYPE html> <html> <head> <title>Titulo</title> <!-- WinJS references --> <link href="//Microsoft.WinJS.0.6/css/ui-dark.css" rel="stylesheet"> <script src="//Microsoft.WinJS.0.6/js/base.js"></script> <script src="//Microsoft.WinJS.0.6/js/ui.js"></script> <!-- Application1 references --> <link href="/css/default.css" rel="stylesheet"> <script src="/js/default.js"></script> </head> <body> <button id="id_createFile">Create</button>

    <button id="id_writeFile"> Write </button> <button id="id_readFile">Read</button> <div id="fileContent"></div> </body> </html>

    default.js

    (function () { "use strict"; var sampleFile = null; var fileExist = false; function id(elementId) { return document.getElementById(elementId); } function initialize() { Windows.Storage.KnownFolders.documentsLibrary.getFileAsync("data.txt").done( function (file) { sampleFile = file; fileExist = !fileExist; }, function (err) { // (1) //fn_createFile(); //fn_writeFile(); //fn_readFile(); } ); // (2) id("id_createFile").addEventListener("click", fn_createFile, false);

    id("id_writeFile").addEventListener("click", fn_writeFile, false); id("id_readFile").addEventListener("click", fn_readFile, false); } function fn_createFile() { Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("data.txt", Windows.Storage.CreationCollisionOption.openIfExists).done( function (file) { sampleFile = file; id("fileContent").innerHTML = "create OK"; } ); } function fn_writeFile() { var conteudoDefault = " Test --- something ... blah blah "; Windows.Storage.FileIO.writeTextAsync(sampleFile, conteudoDefault).done(function () { id("fileContent").innerHTML = "write OK"; }); } function fn_readFile() { if (sampleFile !== null) { Windows.Storage.FileIO.readTextAsync(sampleFile).done( function (fileContent) { id("fileContent").innerHTML = fileContent; } ); } } document.addEventListener("DOMContentLoaded", initialize, false); })();


    If I click in CREATE, my file is create , then, if I click in WRITE, something is write in my file , and then , if I click in READ, my app show me what was written!

    everything is fine .. BUT .. I cant use buttons ! I need to call All this methods when my applications shows .. 

    but if I do like in (1) .. I get an error !!

    I don't know what to do .. because if I use buttons (2), run perfectly , but if I call one by one (1) .. :((((



    • Edited by xazin Sunday, April 22, 2012 12:18 AM
    Sunday, April 22, 2012 12:17 AM

Answers

  • There is also the writeText method, which is a shorthand for doing something similar:

    http://msdn.microsoft.com/en-us/library/windows/apps/Hh700832.aspx

    Cheers,

    Marc

    • Proposed as answer by jrboddie Tuesday, April 24, 2012 8:42 PM
    • Marked as answer by xazin Sunday, May 06, 2012 3:59 AM
    Tuesday, April 24, 2012 5:59 PM
  • Because the three functions are asynchronous, you can't simply call them in series.  You need to use asynchronous chaining to make sure that they execute only after the preceding one has completed.

    Here is an example of the function you are trying to do that works for me:

    var sampleFile;
        function fileAction() {
            Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("data.txt", Windows.Storage.CreationCollisionOption.openIfExists)
            .then(function (file) {
                sampleFile = file;
                var conteudoDefault = "Hello World";
                return Windows.Storage.FileIO.writeTextAsync(sampleFile, conteudoDefault);
            }).then(function () {
                return Windows.Storage.FileIO.readTextAsync(sampleFile);
            }).done(function (fileContent) {
                document.getElementById("status").innerText = fileContent;
            });
        }

    For more information see:

    Quickstart: Using promises in JavaScript

    and

    Chaining promises in JavaScript

    Sunday, April 22, 2012 4:03 AM

All replies

  • Because the three functions are asynchronous, you can't simply call them in series.  You need to use asynchronous chaining to make sure that they execute only after the preceding one has completed.

    Here is an example of the function you are trying to do that works for me:

    var sampleFile;
        function fileAction() {
            Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("data.txt", Windows.Storage.CreationCollisionOption.openIfExists)
            .then(function (file) {
                sampleFile = file;
                var conteudoDefault = "Hello World";
                return Windows.Storage.FileIO.writeTextAsync(sampleFile, conteudoDefault);
            }).then(function () {
                return Windows.Storage.FileIO.readTextAsync(sampleFile);
            }).done(function (fileContent) {
                document.getElementById("status").innerText = fileContent;
            });
        }

    For more information see:

    Quickstart: Using promises in JavaScript

    and

    Chaining promises in JavaScript

    Sunday, April 22, 2012 4:03 AM
  • There is also the writeText method, which is a shorthand for doing something similar:

    http://msdn.microsoft.com/en-us/library/windows/apps/Hh700832.aspx

    Cheers,

    Marc

    • Proposed as answer by jrboddie Tuesday, April 24, 2012 8:42 PM
    • Marked as answer by xazin Sunday, May 06, 2012 3:59 AM
    Tuesday, April 24, 2012 5:59 PM
  • thx ! jrboddie and Marc !!!! :D
    Sunday, May 06, 2012 4:00 AM