Hi,
i've been struggling to look for an example that saves changes to data thats been altered on a window.
I basically want the user to have the ability to create a project, add images and notes onto a viewer, then save for when returning and loading the project later on.
I have done research and looked for examples but none have really worked. I'm not sure whether to use MySql or not - Say, for saving when you scale or position an object - to be able to save the x and y coordinates so that when the user returns to the project, the coordinates have been updated.
I imagine this would entail using the app.properties module as others have suggested - using one of the 'set' functions? I also thought of using the saveTo function, but this doesn't seem to exist in the documentation anymore nor does it seem to be considered a function and I receive an error when using it. I'm using Titanium Developer 1.2.2.
Having looked at KitchenSink, the closest example I could find was the properties example, but this only writes data to a .txt file rather saving changes on the original file - Unless I'm looking at the wrong example or have misunderstood something.
At the moment this is what I have and am really seeking an alternative, or just an all in one example that creates and saves changes to a file.
save_btn.addEventListener('click', function() { parent = Ti.Filesystem.getresourcesDirectory(); var newDir = Titanium.Filesystem.getFile(parent + '/pages' + '/notepad'); if( !newDir.exists() ) { newDir.createDirectory(); } var f = Titanium.Filesystem.getFile(newDir.nativePath,'add_text4.js'); var newFile = Titanium.Filesystem.getFile(newDir.nativePath,'add_text4.js'); newFile.createFile(); //newFile.write(f.read()); f.saveTo('../pages/notepad/add_text4.js'); Ti.API.info("doc saved"); });
Thanks in advance if anyone can help.
1 Answer
Accepted Answer
> [...] am really seeking [...] an all in one example that creates and saves changes to a file.
Here you go:
/* * On app start, a window with a textarea and a save button is created. The * textarea is filled with content read from a file (if it exists). * * Clicking on the save button saves the textarea content to the file. The next * time the app is started, that content will be used to fill the textarea. */ var window = Ti.UI.createWindow({ backgroundColor: "#FFF", exitOnClose: true, fullscreen: false, layout: "vertical" }); var textArea = Ti.UI.createTextArea({ height: 100, left: 10, right: 10, top: 10, value: loadTextFromFile() }) window.add(textArea); var button = Ti.UI.createButton({ height: 40, left: 10, right: 10, title: "Save", top: 10 }); button.addEventListener("click", function() { // Save textarea content in file var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "foo.txt"); file.write(textArea.value); // Unfocus textarea to hide keyboard textArea.blur(); }); window.add(button); window.open(); /** * Returns the content of a file named "foo.txt" located in the application data * directory. If that file does not exist, an empty string is returned. */ function loadTextFromFile() { var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "foo.txt"); return file.exists() ? file.read().text : ""; }
Your Answer
Think you can help? Login to answer this question!