В одном проекте я могу написать файл, но в другом я не могу, действительно странно - PullRequest
0 голосов
/ 23 января 2012

В одном проекте мне удалось настроить запись файла с помощью функции phonegap:

$("#download").live("click", function(){
                            writeFile();
                            });

function writeFile(){

            $.get("http://www.bartdekimpe.be/anoire/index.php/admin/getGamesUserJson/34", function(result){
                  json = result;
                  removeHTMLTags();
                   window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
                  });

}


function removeHTMLTags(){
            if(json){
                var strInputCode = json;
                /* 
                 This line is optional, it replaces escaped brackets with real ones, 
                 i.e. < is replaced with < and > is replaced with >
                 */ 
                strInputCode = strInputCode.replace(/(&nbsp;)*/g,"");
                strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
                                                    return (p1 == "lt")? "<" : ">";
                                                    });
               // strTagStrippedText = '"';
                strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
                strTagStrippedText = $.trim(strTagStrippedText); // overbodige spaties weghalen
                //strTagStrippedText += '"';
            }};

function gotFS(fileSystem) {
            fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail); 
        }

        function gotFileEntry(fileEntry) {
            fileEntry.createWriter(gotFileWriter, fail);
        }

        function gotFileWriter(writer) {
            writer.onwrite = function(evt) {
                console.log("write success");
            };
            writer.write(strTagStrippedText);


            readFile();
            // contents of file now 'some sample text'
           // writer.truncate(11);
            // contents of file now 'some sample'
           // writer.seek(4);
            // contents of file still 'some sample' but file pointer is after the 'e' in 'some'
            //writer.write(" different text");
            // contents of file now 'some different text'
        }
        // Einde FILE WRITER

        function fail(error) {
            console.log(error.code);
        }

В другом проекте я делаю то же самое, и это не сработает, я создал другой проект, потому что мне нужно было начинать все сначала. Я делаю это так:

$(".startNew").live("click", function(){
                            writeFile();      
                            $.mobile.changePage($("#games"));
                            krijgSpellen();


                              });

function writeFile(){
            navigator.notification.alert("write file");
            $.get("http://www.bartdekimpe.be/anoire/index.php/admin/getGamesUserJson/34", function(result){
                  json = result;
                  removeHTMLTags();
                  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
                  });


        }

        function removeHTMLTags(){
            if(json){
                var strInputCode = json;
                /* 
                 This line is optional, it replaces escaped brackets with real ones, 
                 i.e. < is replaced with < and > is replaced with >
                 */ 
                strInputCode = strInputCode.replace(/(&nbsp;)*/g,"");
                strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
                                                    return (p1 == "lt")? "<" : ">";
                                                    });
                // strTagStrippedText = '"';
                strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
                strTagStrippedText = $.trim(strTagStrippedText); // overbodige spaties weghalen
                //strTagStrippedText += '"';
            }};


 function gotFS(fileSystem) {
            fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail); 
        }

        function gotFileEntry(fileEntry) {
            fileEntry.createWriter(gotFileWriter, fail);
        }

        function gotFileWriter(writer) {
            writer.onwrite = function(evt) {
                console.log("write success");
            };
            writer.write(strTagStrippedText);


            readFile();
            // contents of file now 'some sample text'
            // writer.truncate(11);
            // contents of file now 'some sample'
            // writer.seek(4);
            // contents of file still 'some sample' but file pointer is after the 'e' in 'some'
            //writer.write(" different text");
            // contents of file now 'some different text'
        }
        // Einde FILE WRITER

        function fail(error) {
            console.log(error.code);
        }

Это не будет записывать файл, действительно странно, когда я сделаю это:

$(".startNew").live("click", function(){
                            writeFile();      
                            $.mobile.changePage($("#games"));
                            krijgSpellen();


                              });

файл не записывается, но когда я делаю это:

$(".startNew").live("click", function(){
                            writeFile();      
                              });

Работает и пишет файл

1 Ответ

0 голосов
/ 23 января 2012

из вашего комментария выше кажется, что вы генерируете ссылку с классом .startNew динамически, поэтому она работает с live, если вы используете jquery 1.7 + , используйте on или используйте delegate, потому что live устарело

$(function(){
 $(document).on("click",".startNew", function(){ writeFile(); });
});

или с делегатом

$(function(){
     $(document).delegate(".startNew","click", function(){ writeFile(); });
    });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...