tags:

views:

463

answers:

1

Once a Native Context Menu is invoked, I need it to be populated from a set of remote service calls. Current implementation, however, requires items to be set before opening the menu.

Any ideas?

Thanks ;) A

A: 

EDIT: I've once again re-read your question and realized that you want to update a menu while you are opening it, not before it is opened. There is another event you can listen for called displaying but in the test I've done with my HTML/JS test app it seemed that event did not fire. If you are using Flex you could investigate that, or figure out if I've done something wrong here:

 window.htmlLoader.contextMenu = new air.NativeMenu();
 menu1 = new air.NativeMenu();
 loading=new air.NativeMenuItem("Loading...");
 menu1.addItem(loading);
 menu1.addEventListener(air.Event.DISPLAYING, addMenu1Items);
 function addMenu1Items(){
  menu1.removeItem(loading);
  item11 = new air.NativeMenuItem("Item 1");
  item11.addEventListener(air.Event.SELECT, function(e){alert("You clicked Menu 1,Item 1");e.preventDefault();});

  item12 = new air.NativeMenuItem("Item 2");
  item12.addEventListener(air.Event.SELECT, function(e){alert("You clicked Menu 1,Item 2");e.preventDefault();});

  menu1.addItem(item11);
  menu1.addItem(item12);
 }

I've just done a test with an HTML / Javascript app, and the following seems to work for me ( shamelessly ripped and modified from: http://www.adobe.com/devnet/air/ajax/quickstart/adding_menus.html and if you happen to be using Flex, there's a version for that as well: http://www.adobe.com/devnet/air/flex/quickstart/adding_menus.html ).

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title>Context</title>
    <script src="lib/air/AIRAliases.js" language="JavaScript" type="text/javascript"></script>
    <script type="text/javascript">
     window.htmlLoader.contextMenu = new air.NativeMenu();
     menu1 = new air.NativeMenu();

     item11=new air.NativeMenuItem("Item 1");
     item11.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu 1,Item 1");e.preventDefault();});

     item12=new air.NativeMenuItem("Item 2");
     item12.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu 1,Item 2");e.preventDefault();});

     menu1.addItem(item11);
     menu1.addItem(item12);

     menu2 = new air.NativeMenu();
     item21=new air.NativeMenuItem("Item 1");
     item21.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu 2,Item 1");e.preventDefault();});

     item22=new air.NativeMenuItem("Item 2");
     item22.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu 2,Item 2");e.preventDefault();});

     menu2.addItem(item21);
     menu2.addItem(item22);

     window.htmlLoader.contextMenu.addSubmenu(menu1,"Menu 1");
     window.htmlLoader.contextMenu.addSubmenu(menu2,"Menu 2");

     //Displays the context menu
     function showContextMenu(event){
      event.preventDefault();
      window.htmlLoader.contextMenu.display(window.nativeWindow.stage, event.clientX, event.clientY);
     }

     function addItemToContextMenu(){
      menux = new air.NativeMenu();
      item=new air.NativeMenuItem("Item &1");
      item.addEventListener(air.Event.SELECT,function(e){alert("You clicked Menu X,Item 1");e.preventDefault();});
      menux.addItem(item);
      menux.addItem(new air.NativeMenuItem("Item &2"));
      window.htmlLoader.contextMenu.addSubmenu(menux,"Menu X");
     }


     function clearAndAdd() {
      window.htmlLoader.contextMenu = new air.NativeMenu();
      addItemToContextMenu();
     }
    </script>
</head>
<body id="body">
<div id="contextDiv"  oncontextmenu="showContextMenu(event)">
Right click to bring up the context menu.<br><br>
<input type="button" value="Add Item To Context Menu" onclick="addItemToContextMenu()"><input type="button" value="Clear Menu and Add Item to Context Menu" onclick="clearAndAdd();">
</div>
</body>
</html>
Peter Richards
Peter,Thanks for the thorough explanation.Now, the prob is that the menu tells you it is displaying and requires you to set the items immediately. There is apparently no way to tell it to *wait* until an async reply arrives, which is my case. Menu items are dynamic and depend on external logic.

related questions