Acknowledged

RFC: Add callback to MenuItem or Menu2 to remove dependency on delegates

I would like to write less code for simple menus that just set/change a configuration item. I now need to write delegates to do simple things and need to pass around objects to make sure a delegate has access to it. I would like to write something more concise  like this:

var menu = new Ui.Menu2({
  :title => "Some title",
});

menu.addItem(
  new Ui.menuItem(
    "Some label",
    "",
    :id,
    { callback => object.invoke(:method, [ args ]) }
  ),
);

var itemId = menu.findItemById(id);
var item   = menu.getItem(itemId);
item.setCallback(object.invoke(:method, [ args ]);
menu.updateItem(item, itemId);

// or maybe, and I prefer this over the former

menu.addItem(
  new Ui.menuItem(
    "Some label",
    "",
    :id,
    null,
  ),
  object.invoke(:method, [ args ]),
);

var itemId = menu.findItemById(id);
menu.setCallbackOnItem(itemId, object.invoke(:method), [ args ]);

Parents
  • Have you looked at the MenuTest and Menu2Sample in the SDK?  In what you posted, you don't seem to be pushing a view and have no way to get user input (up/down/select).  You can always build your own UI if you don't like menu/menu2

    With Menu2, when onSelect is called in the delegate, you know which item was select ed and do whatever you want when that happens.  Same with onMenuItem in Menu.

Comment
  • Have you looked at the MenuTest and Menu2Sample in the SDK?  In what you posted, you don't seem to be pushing a view and have no way to get user input (up/down/select).  You can always build your own UI if you don't like menu/menu2

    With Menu2, when onSelect is called in the delegate, you know which item was select ed and do whatever you want when that happens.  Same with onMenuItem in Menu.

Children
  • I know, which is why I want the callback because my delegates are basically this;

    function onSelect(item) {
    
      var itemId = item.getId();
    
      var mapping = {
        :idx   => 1,
        :idy => 3,
        :idz  => 5,
      };
    
      self.config.setSomeConfig(mapping.get(itemId));
      UI.popview(..);
      return;
    }

    I want to remove that whole blob and replace it with

    menu.addItem(
      new Ui.menuItem(
        "Some label",
        "",
        :idx,
        null,
      ),
      object.invoke(:setConfigItem, [ 1 ]),
    );
    

    We can get rid of 30 lines of code by using this callback functionality. If you want more elaborate things sure, use a delegate. But these simpel config menu's can be soo much smaller in terms of footprint.