0 Replies Latest reply on Mar 27, 2007 12:49 PM by jllaird02

    Javascript event handling

    jllaird02
      The following code is being executed three times in the following sequence:
      1) On initialization of my Flex application, initCallback() is called
      2) When I broadcast a 'getUserPreferencesFromCookie' event, handleGetUserPreferenceCookie() is called.
      3) When I broadcast a 'writeUserPreferencesCookie' event, handleWriteUserPreferenceCookie() is called.

      // Callback when flash is initialized
      var initCallback = function()
      {
      // Add all event listeners
      FABridge.flash.root().getPreferences().addEventListener(
      "getUserPreferencesFromCookie", handleGetUserPreferenceCookie);
      FABridge.flash.root().getPreferences().addEventListener(
      "writeUserPreferencesCookie", handleWriteUserPreferenceCookie);

      FABridge.flash.root().getPreferences().faBridgeInitialized();
      }

      // Listen for flash initialization (i.e. the Main application is created)
      FABridge.addInitializationCallback("flash", initCallback);

      // Event handler for Get User Preferences Cookie Events events
      var handleGetUserPreferenceCookie = function(readEvent)
      {
      alert("Getting Cookie for user: " + readEvent.getUsername());

      if (readEvent.getUsername())
      {
      var theCookie = readCookie(readEvent.getUsername());
      alert("Returned from readCookie: '" + theCookie + "'");
      var preference = FABridge.flash.root().getPreferences();
      if (preference)
      {
      if (preference.setPreferenceCookie)
      {
      preference.setPreferenceCookie(theCookie);
      }
      }
      }
      else
      {
      FABridge.flash.root().getPreferences().setPreferenceCookie(null);
      }
      }

      // Event handler for Write User Preferences Cookie Events events
      var handleWriteUserPreferenceCookie = function(writeEvent)
      {
      alert("Creating Cookie for User");
      if (writeEvent)
      {
      alert("Write event is defined");

      var username = writeEvent.getUsername();
      if (username)
      {
      // Create a cookie given the username and the XML
      createCookie(writeEvent.getUsername(), writeEvent.getData());
      alert("Returned from creating cookie");
      }
      }
      else
      {
      alert("Write event is undefined");
      }

      alert("Write cookie method completed");
      }

      Here is the problem:

      handleGetUserPreferences() completes as expected (i.e. the cookie string is set into my preferences object). The readEvent is a custom event that extends flash.events.Event. However, when writeUserPreferenceCookie is called, the line writeEvent.getUsername() fails. writeEvent is the same type of event as readEvent. And when I say 'fails' I mean that the method goes no further. I never see the 'Write cookie method completed' alert. There are no errors in the Javascript Error Console.

      I've checked the writeEvent on the action script side. The username has been set into the event the same way that it was set into the readEvent. I cannot see why I can get data out of the readEvent but not the writeEvent.

      ANY help would be much appreciated.

      -Julie