3 Replies Latest reply on Dec 7, 2008 1:53 PM by Barry.van.veen

    Dispatching custom event

    Barry.van.veen
      Dear all,

      In the application I'm building I create a new nativeWindow which contains a simple form that the user has to fill in. When the form is submitted I try to dispatch a custom event. The idea is that the original window catches this event and processes the data from the form. Sadly I could not really find a good answer in the documentation so I hope you can help me.

      This is the code of my original application window:
      quote:


      var windowOptions = new air.NativeWindowInitOptions();
      var newWindow = air.HTMLLoader.createRootWindow(true, windowOptions, false);
      newWindow.window.nativeWindow.alwaysInFront = true;

      newWindow.addEventListener(CUSTOM_EVENT_HERE, receiveData);

      newWindow.load(new air.URLRequest("message.html"));

      function receiveData(event) {

      var test = event.target.data;
      air.trace('window returned: "' + test + '"');

      }




      And this is the code of the new nativeWindow:

      quote:




      var myEvent = new air.Event(CUSTOM_EVENT_HERE, true, false);
      air.NativeApplication.activeWindow.dispatchEvent(myEvent);





      The problem is that I don't know how to define the custom event, and, how to dispatch the event from the nativeWindow so the eventListener will catch it. Hope you can help.

      Thanks,

      Barry
        • 1. Re: Dispatching custom event
          Joe ... Ward Level 4
          Your general outline seems sound. Replace CUSTOM_EVENT_HERE with a string that gives your event some arbitrary name.

          Also, "air.NativeApplication.activeWindow.dispatchEvent(myEvent);" should just be: window.nativeWindow.dispatchEvent(myEvent);
          • 2. Dispatching custom event
            Barry.van.veen Level 1
            This still doesn't work. I get the error "TypeError: Undefined value" for the line
            "var myEvent = new air.Event('loaded', true, false);".

            But, it also gives this error when I try to dispatch
            "window.NativeWindow.dispatchEvent(air.Event.COMPLETE);"
            which should be pretty standard, right?

            Does anyone have any idea what's setting off these errors?
            • 3. Re: Dispatching custom event
              Barry.van.veen Level 1
              After a lot of searching in the docs and this forum I found an easier way. To communicate from the new window to its parent (and to call any function) you can use window.parent.YOUR_FUNCTION();.

              Hopefully this will save someone else some time because i've been searching for hours and still had to find it myself...