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
  • 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.

Comment
  • 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.

Children
No Data