zomgistania

Main page | About | Articles | Previous Posts | Archives

Monday, August 21, 2006

Moving settings over from an old widget to a new one


In Opera Widgets you can save settings using the widget.setPreferenceForKey method. It's very useful, but there's a small flaw in the design.

When you release a new version of your widget, the user must download the new version of the widget and they usually have to reconfigure it as the preferences you stored in the old widget do not move over to the new one.


Here's a very simple method to move the settings over from the old version to the new version. It's not completely automatic, but it shouldn't be too difficult or troublesome for the user.


First, you need a field in the widget. It can be an input or a textarea. This will be used to display the widgets internal data for the preferences so the user can copy it.


Getting the data to the field is very simple. You can use the JavaScript object serializer I introduced in an earlier post to create a text version of the array or the object you use to store the settings.

If you don't use an object or an array to store the settings, I'd recommend you start using one because it simplifies things a lot if there's much data.


Second, you need a button for loading the data in the field. This is used to have the widget load the data in the field.

The way this works is that the user has both the new and old version running, he goes to the field in the old one, copies the data there and pastes it into the new one and presses the load button. The new version then just uses the data in the field and loads itself instead of using the data stored in the widget.preferenceForKey thing. Simple huh?


I also wrote a small example widget to demonstrate the idea. Grab it from here. If it doesn't run as a widget, just load it from File->Load.

Put some values to the two value fields, press save. Then it should reload the widget and display the data string in the first field. You can then copy that and set some new values in the two value fields and save again.

Then just paste the string you copied from the first field back to the first field and press load... you should see how the two fields below revert to what they were when you copied the string :)


As usual, if anything was unclear or you have any questions, ask away!

Labels: , ,

2 Comments:

  • What happens when the user inserts malicious data into the text field? Do you need some kind of validation or is that not something you have to worry about?

    By Anonymous Anonymous, at 12:36 PM  

  • It's not possible to cause any harm no matter what you insert there.

    By Blogger Jani, at 12:39 PM  

Post a Comment

<< Home