You want to have your user interface in the Klip's setup window reflect what is contained in a data source, and keep the information up to date.
In this example, we'll dynamically configure the contents of a combobox. The main idea is that we will make two requests during the onRefresh handler, and that each request will use different onCreate and onUpdate handlers to manipulate the data. Please note that this example assumes that the configuration data source and the regular data source share similar tag naming in their XML (different stylesheets could be used for each data source by using Engines.Data.stylesheet).
First of all, we'll create a "Setup" tab with a combobox on it:
function onLoad()
{
// Build the Setup window UI
Setup.addTab("Setup");
Setup[0].addText("Select which icon colour to use:");
gCombo = Setup[0].addComboBox();
}
function onRefresh()
{
// Use special onCreate and onUpdate handlers for the configuration items
Engines.Data.onCreate = configCreate;
Engines.Data.onUpdate = configUpdate;
// Clear the arrays that will contain the configuration options
gColours = new Array();
gImages = new Array();
var req = Engines.HTTP.newRequest("http://www.myserver.com/feeds/myfeed.xml");
if(req.send())
{
if(req.response.data.length > 0)
{
// Process the configuration data
Engines.Data.process(req.response.data);
}
}
// Build the combobox out of the configuration data
buildCombo();
// Use different onCreate and onUpdate handlers for the regular items
Engines.Data.onCreate = myCreate;
Engines.Data.onUpdate = myUpdate;
return Engines.Data.process(Prefs.contentsource);
}
function configCreate(newItem)
{
var colour = newItem.getData("status");
var image = newItem.getData("description");
gColours.push(colour);
gImages.push(image);
// Return false to make sure the item isn't created in the Klip
return false;
}
function configUpdate(oldItem, properties)
{
return configCreate(properties);
}
The above functions read the data from the configuration feed as each item is processed, and save the data in the two arrays. Note that false is returned from this function, preventing the configuration items from appearing in the Klip.
Once the configuration data has been read, the onRefresh function calls buildCombo():
function buildCombo()
{
// Reconstruct the combobox based on the new data feed.
gCombo.clear();
var i = 0;
for(i = 0; i < gColours.length; i++)
{
gCombo.addItem(gColours[i]);
}
}
gImages[gCombo.selected]