0 Replies Latest reply on May 11, 2010 3:04 PM by chris.s.jordan

    Trouble with addEventListener()...

    chris.s.jordan

      I'm having trouble with addEventListener()... I think.

       

      I'm building an app that is a system tray icon with a menu. Right-click the icon, choose an option from the menu and cool stuff happens. One of the options opens a configuration screen. This configuration screen allows the user to add server names and addresses. These server names are then stored in a file along with the addresses, and added to a dynamic submenu. In other words, the user configures the app to know about three servers, and this dynamic submenu will contain those three servers.

       

      The problem comes when adding the event listener at the time of menu creation. The menu creation happens just fine and items are added or removed from this dynamic submenu without problem... but the event listener isn't working.

       

      However, if I restart the application (which reads the configuration from the drive, and builds the submenu using the **exact same code**), the event listeners work just fine! I'll reiterate -- the code which builds the submenu on application launch is the same code that dynamically rebuilds the submenu on-the-fly.

       

      Here is the relavent code:

       

      addReinitSubmenu: function(jqe, menu){
           if(!menu){
                menu = air.NativeApplication.nativeApplication.icon.menu;
           }
           //always want the Reinit menu to be at the top, so add it at position 0
           menu.addSubmenuAt(MenuManager.buildReinitSubmenu(menu),0,"Reinit");
      },
      buildReinitSubmenu:     function(menu){
           var my = {};
           //look for the "Reinit" menu item
           my.foundReinitMenu = false;
           if (menu) {
                for (my.i = 0; my.i < menu.items.length; my.i++) {
                     if (menu.items[my.i].label === 'Reinit') {
                          my.foundReinitMenu = true;
                          // store this index position for later.
                          my.ReinitMenuIndex = my.i;
                          //store a handle to the Reinit submenu
                          my.ReinitMenu = menu.items[my.i].submenu;
                          break;
                     }
                }
           }
           if(my.foundReinitMenu){
                //we found the reinit submenu so destroy it before we rebuild it.
                //start by unbinding the event listeners in it...
                for (my.i = 0; my.i < my.ReinitMenu.items.length; my.i++){
                     my.ReinitMenu.items[my.i].removeEventListener(air.Event.SELECT, MenuManager.onReinitSelect);
                }
                //then remove all items from the menu
                my.ReinitMenu.removeAllItems();
                //now remove the Reinit Menu from the main menu
                menu.removeItemAt(my.ReinitMenuIndex);
           }
           //now start (re)building the Reinit subMenu
           my.submenu = new air.NativeMenu();
           if(window.AppConfig.config.length > 0){
                for(my.i = 0; my.i < window.AppConfig.config.length; my.i++){
                     my.submenu.addItem(new air.NativeMenuItem(window.AppConfig.config[my.i].servername));
                     my.submenu.items[my.i].addEventListener(air.Event.SELECT,MenuManager.onReinitSelect);
                }
           }
           else{
                my.emptyItem = new air.NativeMenuItem("Empty");
                my.emptyItem.enabled = false;
                my.submenu.addItem(my.emptyItem);
           }
           return my.submenu;
      },
      onReinitSelect: function(e){
           em.dispatchEvent("MenuManager_CBReinit", e);
      },

       

      I should note here that I'm writing this in JavaScript using jQuery.

       

      I am not getting any errors, just unexpected behavior. As a debugging measure, I even put a trace after the addition of the event listener to check the value of my.submenu.items[my.i].hasEventListener(air.Event.SELECT)... and it's true both at application launch, and after destroying / recreating the menu after the user changes the configuration data.

       

      I'm stumped.

       

      Any help would be appreciated.