Sharing Data
You can get GoLive user preferences programmatically, and also create your own extension preferences. You can set values that persist across user sessions, or that are available to other extension only during a single session. You can also communicate with other running extensions and share data with them.
- Extension modules can use a prefs Object (page 240) to store persistent user preference data that is available to all extensions. The attribute Object (page 62) provides read-only access to a subset of GoLive’s global preferences.
- The common Object (page 77) provides a place to share non-persistent data with other extensions.
- The app Object’s broadcast method (page 56) allows one extension to send a message to another, and optionally receive a reply from that extension’s broadcast event handler (page 360).
You cannot share or save objects, which are ephemeral. The broadcast method can only exchange strings with other extensions.
Persistent shared data
Extensions can create their own preference data, which persists across between user sessions and is available to all extension modules. This feature enables all extensions to share a common set of preferences and to store persistent data.
The prefs Object (page 240) in the prefs global variable (page 48) makes preference data available to all extensions. Preferences are stored as properties of this object. Create a new preference value simply by writing to a prefs property. For example, this creates a myModule preference that holds "Version 1.0" as its value:
prefs.myModule = "Version 1.0";
All other modules can check for the presence of this module:
if (prefs.myModule == "Version 1.0")...
When GoLive quits, it saves the prefs values along with all other preference data. The next time GoLive starts, it makes this data available to all modules again.
Non-persistent shared data
The common Object (page 77) provides a means of sharing non-persistent string and primitive data among all currently running extensions. You cannot store objects in the common object.
The common object provides no predefined properties. Your extension can call the common object’s create method to create a new namespace for shared properties in the common object; the namespace is then available to all extensions as a property of the common object.
For example, the following creates a namespace to hold properties of a particular extension, then adds a property with an assigned value:
common.create("myExt"); common.myExt.myProperty = myValue;
Communicating with other extensions
The broadcasting mechanism enables an extension to send a message to another extension requesting that it perform a task. Optionally, the called extension can return a result string.
When one extension module needs to communicate with another extension module, it calls the app Object’s broadcast method (page 56). This method generates a broadcastEvent (page 378), containing the message argument. An extension that responds to messages from other extensions defines a handler for the broadcastEvent and registers it in the module Object (page 227).
Your handler for the broadcastEvent checks the message property, performs any tasks necessary to respond to the broadcast message, and optionally returns a result string in the answer property of the event object.
Use of the broadcast mechanism is entirely optional. If your extension does not need to communicate with other extensions, it need not implement a broadcastEvent handler.
Sending messages to other extension modules
The broadcast method sends its argument to the broadcastSignal method of any or all running extension modules.
broadcast (argument [, targets]);
This method converts the argument value to a string, stores the name of the calling module and the argument string to a broadcastEvent object, and generates this event in the target module or modules. The optional targets value is a list of modules to call, in calling order. By default, the broadcast method generates the event in every extension module.
Each called module’s registered broadcastEvent handler must return a string value or undefined. Broadcasting terminates as soon as any extension’s handler returns a value. (The handler can also store a response in the answer property of the event object.)
This method’s result is the return value supplied by the last handler. If none of the called modules supply a return value, the broadcast method’s return value is undefined.
Responding to a broadcast
To receive and respond to messages from other extensions, your extension must define a handler for the broadcast event (page 360) and register it with the app object. A called module’s handler determines its actions according to the message and sender values in the passed broadcastEvent object (page 378). The value the handler sets in the object’s answer property is returned as the broadcast method’s return value. For example:
function handleBroadcast(bcastEvt) { switch (bcastEvt.message) { case thisValue: bcastEvt.answer=5; case thatValue: bcastEvt.answer="Yes"; case anotherValue: { var myResult = myFunction(); if (myResult) bcastEvt.answer=myResult.toString() else bcastEvt.answer="undefined"; } default: bcastEvt.answer="undefined"; } } //register handler function initializeModule(){ app.addEventListener( 'broadcastSignal', handleBroadcast) }