Building custom gadgets : Linking Google gadgets : Using multiple import gadgets
 
Using multiple import gadgets
Dashboard developers can use multiple Google gadgets on the same dashboard. Each Google gadget must be listening on unique channel names unless the gadget developer wants them to receive the same user selections. When an import gadget links to a data selection gadget, the message sent by the data selection gadget is a global message. This global message is sent across the dashboard on the ON_SELECTOR_GADGET_CHANGED channel. This means that when a user selects a value from a a data selection gadget, all linking import gadgets that listen on the ON_SELECTOR_GADGET_CHANGED channel receives the same message.
For example, a dashboard developer adds an import gadget to show a map of customer addresses and another import gadget to show shipping status on orders. When a user selects the order number from a linked list gadget, both import gadgets receives the value and try to process it. The import gadget that shows shipping can process the value. The import gadget showing a map expects an address and is unable to retrieve a valid map from an order number.
The issue is resolved when both import gadgets listen on different channels for user selections. Once each gadget is assigned a unique channel event name, messages can go to the correct import gadget. If the previous example used unique channel names, changes to the customer list gadget only affects the import gadget showing a map. Changes to the list gadget showing order numbers, only affects the import gadget showing shipping status.
Using a unique channel name
The gadget developer can create a unique channel name in the Google gadget and configure the import gadget to use the new channel name when linking to the data selection gadget.
To use a unique channel name in a Google gadget, change the channel name when calling the gadgets.pubsub.subscribe() method. The following code shows the channel name changed to Unique_channel_name:
gadgets.pubsub.subscribe("Unique_channel_name", onEventChange);
Only data arriving on the unique channel name triggers the onEventChange function.
Changing a channel name
Once the Google gadget is listening for a unique channel name, the channel name must be changed in the dashboard. Add a script to the link settings of the import gadget to match the channel name used in the embedded Google gadget. The following code shows an example of this script that changes the channel event name to Unique_channel_name.
data.event = 'Unique_channel_name';
This script overrides the default event name used by the gadget. The unique channel name must match the name that the embedded Google gadget is listening for.
Figure 8‑4 shows JavaScript in a link event for an import gadget.
Figure 8‑4 Changing the channel event name
All import gadgets listening for the new channel name Unique_channel_name receives the messages from their list gadget.
For more information about adding script to a linking gadget, see Scripting linked gadgets.