Partager via


Persistent Gadget Settings

Here is a question that seems to pop up with some regularity on the sidebar newsgroup: "Why doesn't the weather gadget remember where I live, if I close it and then add it again to my sidebar?"

The answer is that every time you add the weather gadget to the sidebar, you are creating a new 'instance' of the gadget; and every instance has its own associated location.  So the newly-added weather gadget has settings that are completely distinct from the settings on the one you closed.  This makes sense when you start to think about having three or five weather gadgets running at the same time - they should each be able to show the weather at a different location.

Having said that, you may choose to make a different design choice in your gadgets.  Unfortunately, the sidebar does not offer a built-in mechanism for sharing settings between gadget instances.  When asked how to do this by other gadget developers, my first response was: "you can use a shared file to share settings between gadget instances."

To make life easier for everyone, I decided to write a javascript library that encapsulated that work, and exposed a simple API for reading and writing shared settings.  In the process of writing the library, I discovered that just saying "use a shared file" vastly oversimplifies the effort involved.  *grin*

I've uploaded the results to the 'Sandbox' section of the Channel 9 site:

Persistent Settings ZIP Archive

The ZIP archive contains a JS file that implements the APIs, and a README.TXT file describing the APIs.  You can include the file in your gadget HTML as follows:

<script src="persistentSettings.js" language="javascript"></script>

I'm including the contents of the README.TXT file below, for ease of reference:

PersistentSettings.js - A Persistent Setting API For Sidebar Gadgets

These APIs allow multiple instances of the same gadget type to share a set of common settings. It will also maintain those settings even when all instances of the gadget are shut down. It will not maintain the settings if the gadget is uninstalled from the gadget gallery window, and then reinstalled.

There are two functions:

setPersistentSetting( name, value [, callback ] )

This function stores the specified name/value pair in the common settings.

Note: You are not guaranteed that your setting is stored until that callback function is called. If you do not specify the optional callback, you must not assume the setting has been written when the function call returns; it has not.

Example:

 setPersistentSetting( "CDName", "Bat out of Hell", setCallback );
function setCallback()
{
    document.body.innerText = "CDName set!";
}

getPersistentSetting( name, callback )

This function gets the value associated with the specified name from the common settings. If the name is not currently present in the shared settings, null is returned.

Note: you must specify a callback function in order to obtain the result of your setting query.

Example:

 getPersistentSetting( "CDName", getCallback );
function getCallback( value )
{
    document.body.innerText = "CDName: " + value;
}

If you have any questions, suggestions or bugs related to these APIs,please contact Bruce.Williams@microsoft.com. I hope you find this useful!

-Bruce

Comments

  • Anonymous
    September 25, 2006
    Soon after I posted my persistent settings library, I received my first bug on it.&amp;nbsp; I'll discuss...

  • Anonymous
    June 16, 2008
    The link above seems to have died, and I'm having some trouble digging up a copy of the code.  If I find it, I'll re-post it.  However, I recommend using the shell scripting APIs to put your persistent data in the registry instead of using this library.  Its a cleaner solution that just didn't occur to me until after I wrote and posted this code.

  • Anonymous
    October 27, 2008
    PingBack from http://mstechnews.info/2008/10/persistent-settings-update/

  • Anonymous
    March 21, 2009
    PingBack from http://designformasters.info/posts/windows-sidebar-gadget/

  • Anonymous
    May 19, 2009
    PingBack from http://demo8.remcom74.ru/?p=403

  • Anonymous
    June 13, 2009
    PingBack from http://thestoragebench.info/story.php?id=8758

  • Anonymous
    July 17, 2011
    Iam trying to log into facebook can you help me?

  • Anonymous
    July 17, 2011
    I would like to be able to log into my old facebook account,can you help me?