• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

13.4. Listenable Shared Objects

The SimplePeopleList component provides a view of the data in a userList shared object by listing the shared object's data properties. Other components may also need to use the userList shared object and be notified when the onSync( ) method is called. In one respect, the SimplePeopleList treats the userList shared object as it treats its List component; it simply adds itself as an event listener for onSync events. Remote shared objects must be modified in order to dispatch events or add and remove event listeners. When an object or component in this chapter needs to connect to a shared object, it uses the SharedObjectFactory class listed in Example 13-2 instead of the SharedObject class. The SharedObjectFactory class retrieves a remote shared object and then adds methods and properties to it so that one or more objects or components can add themselves to the shared object as event listeners. It uses the EventDispatcher class provided as part of the v2 UI component framework to perform part of the customization of each shared object. The code in Example 13-2 must be placed in a file named SharedObjectFactory.as. Read through the heavily commented example if you are interested in how the SharedObjectFactory converts a remote shared object into a v2-style event dispatcher.

Example 13-2. The SharedObjectFactory class

class SharedObjectFactory extends Object {
  /* getRemote( ) calls SharedObject.getRemote( ) to get a shared object reference
   * and then uses the mx.events.EventDispatcher object to make the shared
   * object into a v2 component-style event broadcaster. Also, an isConnected
   * flag is maintained to implement an onFirstSync event and to keep track of 
   * the shared object's connection state. Finally, two methods have been added 
   * to dynamically add and remove remote methods to and from the shared object.
  static function getRemote (name, uri, persistence) {
    // Get the remote shared object.
    var so = SharedObject.getRemote.apply(SharedObject, arguments);
    // If it exists and has not been modified to be a broadcaster...
    if (so && !so._onFirstSync) {
      // Make it an event broadcaster.
      // Define an _onFirstSync( ) method that can be assigned to onSync
      // whenever an attempt is made to connect the shared object.
      so._onFirstSync = function (list:Array) {
        this.isConnected = true;
        this.dispatchEvent({type:"onFirstSync", target: this, list:list});
        this.onSync = this._onSync;
      /* Define an _onSync( ) method to assign to onSync after the first
       * onSync( ) has been called. Note the event.list property contains
       * the list normally passed to onSync( ).
      so._onSync = function (list:Array) {
         this.dispatchEvent({type:"onSync", target: this, list:list});
      // Keep a reference to the SharedObject.connect( ) method.
      so._connect = so.connect;
      // Replace connect( ) with a method that modifies onSync and isConnected
      // before calling the original connect( ) method.
      so.connect = function (nc:NetConnection) {
        this.onSync = this._onFirstSync;
        this.isConnected = false;
      // Keep a reference to the original SharedObject.close( ) method.
      so._close = so.close;
      // Modify the close( ) method to set the isConnected flag before
      // actually closing the shared object.
      so.close = function ( ) {
        this.isConnected = false;
        this._close( );
      /* Dynamically attach a remote method to the shared object. Note
       * the method simply dispatches an event to any interested listeners
       * and includes the arguments object so listeners can deal with the
       * remote call themselves. Listeners must use addEventListener( ) on the
       * shared object to receive remote method call notifications.
       * addRemoteMethod( ) and removeRemoteMethod( ) are discussed later.
      so.addRemoteMethod = function (methodName) {
        so[methodName] = function ( ) {
          this.dispatchEvent({type:methodName, target: this, args:arguments});
      // Delete the remote method if it is no longer needed.
      so.removeRemoteMethod = function (methodName) {
        delete so[methodName];
    return so;




Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint