Persisting custom attributes for an iWidget in IBM Mashup Center

In an earlier entry, I talked through a simple example of how to construct an iWidget for IBM Mashup Center. This afternoon I presented at the WebSphere User Group meeting at IBM Bedfont and was asked about the persistence of state in iWidgets, saving and loading and so on and so it seemed a sensible next step to augment the earlier sample to show how this is done.

ItemSets and attributes

As well as local instance variables inside the Dojo class, the iContext instance provides us with hooks into a standardised structure of instance-specific attributes. These attributes can be defined and set declaratively using the XML descriptor for the iWidget. To extend the earlier example, first modify the XML to contain the following element structure inside the <iw:iwidget> tag:

<iw:itemSet id="attributes" private="false" onItemSetChanged="itemSetChanged">
   <iw:item id="pollInterval" value="5" readOnly="false"/>
</iw:itemSet>

This will make the pollInterval property we coded earlier as an instance variable into a managed attribute inside an ItemSet. The iWidget specification defines the ItemSet data type (and ManagedItemSet sub-type) to describe a generic key-value pair data store for iWidgets. The iContext exposes to us the attributes as an ItemSet instance, with accompanying accessors to get, set and save changes to the values contained within it.

The next step, therefore, is to wire this logic into our existing Dojo class to exploit this feature. At the top of the onLoad method, add the following line:

this.pollInterval = this.iContext.getiWidgetAttributes().getItemValue("pollInterval");

This reads the currently saved (or indeed default) value for the given attribute, and populates our instance variable with the value.

Similarly, our routine to save changes to the iWidget now needs to update the attributes and save the changes so they can be persisted with this instance on the page. Add the following lines after the call to set the pollInterval instance variable in the saveParams method:

this.iContext.getiWidgetAttributes().setItemValue("pollInterval", this.pollInterval);
this.iContext.getiWidgetAttributes().save();

What you will now discover is when you modify the setting and save the page, when the page is reloaded, you will see the attribute load with the modified value.

Advertisements

2 responses to “Persisting custom attributes for an iWidget in IBM Mashup Center

  1. Hi

    I am trying to create a dojo tree (pretty static one). I have a big json style data which needs to be part of the dojo tree. Is there anyway i can use that? can that be included as part of itemset?

  2. Hi

    I am trying to create a dojo tree (pretty static one). I have a big json style data which needs to be part of the dojo tree. Is there anyway i can use that? can that be included as part of itemset?

    Please send me a copy to my email

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s