To main page | Opera Unite HowTo'sJSON State (storing data)See Uniteness for better way to JSON.Saving arbitrary object as JSON to achieve persistence: index.html file: <script src="file_wrap.js"></script> <script src="json.js"></script> <script> var state_filename = 'state.json'; /* will become "/storage/state.json" */ // Load the state var state = JSON.parse( safe_file_get(state_filename, '[]') ); /* [] is default value is file is not found or smth bad happened*/ // so, basically we start with empty list window.onload = function () { webserver = opera.io.webserver if (webserver) { webserver.addEventListener('_index', start_page, false); }; }; function start_page(r) { // Append a number to end of list state.push( state.length ); var o = r.connection.response; o.write('Current state: ' + state); // Save the state safe_file_put(state_filename, JSON.stringify(state) ); o.close(); }; </script>This is suboptimal, due to the fact that state is saved with each request, but so far I haven't found a way to do onUnload. Don't forget to enable fileio library in config.xml. In Unitenessvar db = new JSON_FILE('state_prices.json', []); db.store.push({foo:"bar"}) // CREATE first(0) object db.save() | Last updated
|