×
Namespaces

Variants
Actions
(Difference between revisions)

Storing settings in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
seppo_fn (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
 
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Symbian Web Runtime]][[Category:Code Examples]][[Category:Files/Data]][[Category:S60 3rd Edition FP2]][[Category:S60 5th Edition]]
__NOEDITSECTION__
+
{{Abstract|This code snippet demonstrates how to save persistent data and then restore it when restarting the application by using the methods {{Icode|widget.setPreferenceForKey()}} and {{Icode|widget.preferenceForKey()}}, respectively.}}
{{CodeSnippet
+
 
|id= 
+
{{ArticleMetaData <!-- v1.2 -->
|platform=S60 3rd Edition, FP2, S60 5th Edition
+
|sourcecode= [[Media:SavingPersistentData.diff.zip]] [[Media:Storing settings in WRT.zip]]
|devices=Nokia 5800 XpressMusic
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|category=Web Runtime (WRT)
+
|devices= Nokia 5800 XpressMusic
|subcategory=Files/Data
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|creationdate=January 27, 2009
+
|platform= S60 3rd Edition FP2, S60 5th Edition
|keywords=setPreferenceForKey, preferenceForKey
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= widget.setPreferenceForKey(), widget.preferenceForKey()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090226
 +
|author= [[User:MiGryz]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001409
 
}}
 
}}
  
 
+
==Source: Relevant HTML components==
==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==
+
  
 
<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
 
it. Saved text will be restored to text input field on next launch of the
 
application.
 
  
==Supplementary material==
+
Upon loading, text that corresponds to {{Icode|key}} in the persistent storage is displayed. You can change the text and use the {{Icode|Save}} button to save it. It will be restored the next time the widget is launched.
  
* 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]].
+
==Supplementary material==
  
* 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]]
+
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 to developers. The version of the WRT stub application used as a template in this snippet is v1.1.
  
[[Category:Web Runtime (WRT)]][[Category:Code Examples]][[Category:Files/Data]][[Category:S60 3rd Edition, FP2]][[Category:S60 5th Edition]]
+
* 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 app stubs with logging framework]].

Latest revision as of 09:20, 5 October 2012

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

Article Metadata
Code ExampleTested 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: MiGryz (26 Feb 2009)
Last edited: hamishwillee (05 Oct 2012)

Contents

[edit] 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>

[edit] 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.");
}

[edit] Postconditions

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

[edit] 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 to developers. The version of the WRT stub application used as a template in this snippet is v1.1.

This page was last modified on 5 October 2012, at 09:20.
107 page views in the last 30 days.