×
Namespaces

Variants
Actions
(Difference between revisions)

Storing settings in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
seppo_fn (Talk | contribs)
tapla (Talk | contribs)
Line 8: Line 8:
 
|subcategory=Files/Data
 
|subcategory=Files/Data
 
|creationdate=January 27, 2009
 
|creationdate=January 27, 2009
|keywords=setPreferenceForKey, preferenceForKey
+
|keywords=widget.setPreferenceForKey(), widget.preferenceForKey()
 
}}
 
}}
 
  
 
==Overview==
 
==Overview==
This code snippet shows how to use methods <tt>widget.setPreferenceForKey()</tt>
 
and <tt>widget.preferenceForKey()</tt> to save persistent data and then restore it when restarting
 
the application.
 
  
==Source file: StoreSettings.xhtml==
+
This code snippet shows how to save persistent data and then restore it when restarting the application by using the methods <tt>widget.setPreferenceForKey()</tt> and <tt>widget.preferenceForKey()</tt>, respectively.
 +
 
 +
==Source: Relevant HTML components==
  
 
<code xml>
 
<code xml>
<?xml version="1.0" encoding="UTF-8"?>
+
<div id="bodyContent" class="bodyContent">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+
     <label for="txt">Text:</label>
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
     <input id="txt" type="text"></input>
<html xmlns="http://www.w3.org/1999/xhtml">
+
    <input type="button" onclick="saveString();" value="Save" />
    <head>
+
</div>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+
        <script type="text/javascript" src="script/StoreSettings.js" />
+
        <title>StoreSettings</title>
+
     </head>
+
    <body onload="init( )">
+
    <div>
+
        <input id="txt" type="text"></input>
+
        <input type='button' onclick='dochange( )' value='Save'/>
+
    </div>
+
    </body>
+
</html>
+
 
+
 
</code>
 
</code>
  
==Source file: StoreSettings.js==
+
==Source==
  
 
<code javascript>
 
<code javascript>
/*
+
// Initializes the widget
* This function loads previously saved string and sets it to input field
+
function init() {
*/
+
     // ...
function init( ) {
+
     // Creating new menu item
+
    var save = new MenuItem("Save", 12);
+
   
+
    // Setting handlers for main menu items
+
    save.onSelect = dochange;
+
  
     // Adding items to menu
+
     // Loads the saved string into the input field if the key exists
    menu.append(save);
+
     var savedString = widget.preferenceForKey("key");
 
+
     if (savedString != null) {
    // Start displaying menu
+
         document.getElementById("txt").value = savedString;
    menu.showSoftkeys();
+
   
+
    // Checking existence of key and loadig string to input field if exists
+
     if(widget.preferenceForKey('key'))
+
     {
+
         document.getElementById( 'txt' ).value = widget.preferenceForKey( 'key' );
+
 
     }
 
     }
 
}
 
}
  
/**
+
// Saves the string from the input field to persistent storage.
* Function saves string from input field to system
+
function saveString( ) {
*/
+
     widget.setPreferenceForKey(document.getElementById("txt").value, "key");
function dochange( ) {
+
    alert("String saved.");
     widget.setPreferenceForKey( document.getElementById( 'txt' ).value, "key" );
+
 
}
 
}
 
 
</code>
 
</code>
 
  
 
==Postconditions==
 
==Postconditions==
Upon loading shows text input field.
+
 
You can change text in this field and use "Save" button or menu command to save  
+
Upon loading, shows text that corresponds to "key" in the persistent storage. You can change the text and use the "Save" button to save it. It will be restored when the widget is launched for the next time.
it. Saved text will be restored to text input field on next launch of the  
+
application.
+
  
 
==Supplementary material==
 
==Supplementary material==
  
* You can test the storing of setting in action in a simple, executable application into which this code snippet has been patched. The executables and source files are available for download at: [[Media:Storing_settings_in_WRT.zip]].
+
This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the WRT stub application used as a template in this snippet is v1.1.
  
* You can examine all the changes that are required to implement the above mentioned features in an application. The changes are provided in unified diff and color-coded diff formats: [[link_to_patchs]]
+
* The patched, executable application that can be used to test the features described in this snippet is available for download at [[Media:Storing_settings_in_WRT.zip]].
 +
* You can view all the changes that are required to implement the above-mentioned features. The changes are provided in unified diff and colour-coded diff (HTML) formats in [[Media:SavingPersistentData.diff.zip]].
 +
* For general information on applying the patch, see [[Using Diffs]].
 +
* For unpatched stub applications, see [[Example stub]].
  
[[Category:Web Runtime (WRT)]][[Category:Code Examples]][[Category:Files/Data]][[Category:S60 3rd Edition, FP2]][[Category:S60 5th Edition]]
+
[[Category:Web Runtime (WRT)]][[Category:Code Examples]][[Category:Files/Data]]

Revision as of 11:37, 13 May 2009


Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 3rd Edition, FP2, S60 5th Edition
S60 5th Edition
S60 3rd Edition FP2
Article
Keywords: widget.setPreferenceForKey(), widget.preferenceForKey()
Created: (27 Jan 2009)
Last edited: tapla (13 May 2009)

Overview

This code snippet shows how to save persistent data and then restore it when restarting the application by using the methods widget.setPreferenceForKey() and widget.preferenceForKey(), respectively.

Source: Relevant HTML components

<div id="bodyContent" class="bodyContent">
<label for="txt">Text:</label>
<input id="txt" type="text"></input>
<input type="button" onclick="saveString();" value="Save" />
</div>

Source

// Initializes the widget
function init() {
// ...
 
// Loads the saved string into the input field if the key exists
var savedString = widget.preferenceForKey("key");
if (savedString != null) {
document.getElementById("txt").value = savedString;
}
}
 
// Saves the string from the input field to persistent storage.
function saveString( ) {
widget.setPreferenceForKey(document.getElementById("txt").value, "key");
alert("String saved.");
}

Postconditions

Upon loading, shows text that corresponds to "key" in the persistent storage. You can change the text and use the "Save" button to save it. It will be restored when the widget is launched for the next time.

Supplementary material

This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the WRT stub application used as a template in this snippet is v1.1.

  • The patched, executable application that can be used to test the features described in this snippet is available for download at Media:Storing_settings_in_WRT.zip.
  • You can view all the changes that are required to implement the above-mentioned features. The changes are provided in unified diff and colour-coded diff (HTML) formats in Media:SavingPersistentData.diff.zip.
  • For general information on applying the patch, see Using Diffs.
  • For unpatched stub applications, see Example stub.
108 page views in the last 30 days.