23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

var localSettings = Windows.Storage.ApplicationData.current.localSettings;<br />

var settingName = "exampleSetting";<br />

var settingValue = "Hello World";<br />

function settingsWriteSetting() {<br />

localSettings.values[settingName] = settingValue;<br />

}<br />

function settingsDeleteSetting() {<br />

localSettings.values.remove(settingName);<br />

}<br />

Many settings, like that shown above, are just simple key-value pairs, but other settings will be<br />

objects with multiple properties. This presents a particular challenge: although you can certainly write<br />

and read the individual properties of that object within the values array, what happens if a failure<br />

occurs with one of them? That would cause your state to become corrupt.<br />

To guard against this, the app data APIs provide for composite settings, which are groups of<br />

individual properties that are guaranteed to be managed as a single unit. (Again, each composite has a<br />

64K limit.) It’s like the perfect group consciousness: either we all succeed or we all fail, with nothing in<br />

between! That is, if there’s an error reading or writing any part of the composite, the whole composite<br />

fails; with roaming, either the whole composite roams or none of it roams.<br />

A composite object is created using Windows.Storage.ApplicationDataCompositeValue, as shown<br />

in Scenario 4 of the Application data sample (js/compositeSettings.js):<br />

var roamingSettings = Windows.Storage.ApplicationData.current.roamingSettings;<br />

var settingName = "exampleCompositeSetting";<br />

var settingName1 = "one";<br />

var settingName2 = "hello";<br />

function compositeSettingsWriteCompositeSetting() {<br />

var composite = new Windows.Storage.ApplicationDataCompositeValue();<br />

composite[settingName1] = 1; // example value<br />

composite[settingName2] = "world"; // example value<br />

roamingSettings.values[settingName] = composite;<br />

}<br />

function compositeSettingsDeleteCompositeSetting() {<br />

roamingSettings.values.remove(settingName);<br />

}<br />

function compositeSettingsDisplayOutput() {<br />

var composite = roamingSettings.values[settingName];<br />

// ...<br />

}<br />

The ApplicationDataCompositeValue object has, as you can see in the documentation, some<br />

additional methods and events to help you manage it such as clear, insert, and mapchanged.<br />

319

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!