1

I wonder if it is possible to bind an Event via a JSONModel.

If I do so, it will always throw this Exception:

Uncaught TypeError: I.fFunction.call is not a function

This is my code:

_ViewReference: undefined,
_oMenuItemsConfigModel: undefined,
createMenu: function(oItem){
    if (!this._menu) {
        this._menu = new sap.ui.unified.Menu(this._oMenuConfig);
        this._menu.setModel(this._oMenuItemsConfigModel);

        this._menu.bindAggregation("items", "/", new sap.ui.unified.MenuItem({
            text: "{text}",
            icon: "{icon}",
            select: "{select}",
            enabled: "{enabled}"
        }));

        this._ViewReference.addDependent(this._menu);
    }

    var eDock = sap.ui.core.Popup.Dock;
    this._menu.open(false, oItem, eDock.BeginTop, eDock.BeginBottom, oItem);
    return oItem;
}

I have a Universal ContextMenu which just needs some config in order to get created. This is how I call this function from my Controller:

var oContextMenu = new ContextMenu(this.getView(), 
    new sap.ui.model.json.JSONModel(
        [
            {
                text: "Copy",
                select: [this.onContextMenuItemCopySelected, this]
            },
            {
                text: "Paste",
                select: [this.onContextMenuItemPasteSelected, this]
            }
        ]
    )
);

Here is a JSBin Example.

1 Answer 1

1

You cannot use databinding for events. But you can implement an universal event handler for your menuitems that will call the appropriate function.

Bind the menu items select event to a common event handler:

this._menu.bindAggregation("items", "/", new sap.ui.unified.MenuItem({
                text: "{text}",
                select: [this.onSelect, this]
            }));

And implement the handler like this:

onSelect:function(oEvent){
          var item = oEvent.getParameter("item");
          var context = item.getBindingContext();
          var fnConfig = context.getProperty("select");
          fnConfig[0].bind(fnConfig[1])();

       }

fnConfig is the Array of function an this-object from the model. Using Function.bind() lets you call the function on the given this object.

Here it is on JSBin

Sign up to request clarification or add additional context in comments.

3 Comments

It works in the JSBin, doesn't it? And please mind that i have added the onContextMenuItemCopySelected and onContextMenuItemPasteSelected functions to the controller in the JSBin example. Do you have some differences to your JSBin example in your real code?
nope of course it works! But i seriously dont get it why it doesn't work with a binding! Nevertheless, this is a great work around.
SAP has implemented databinding only for properties associations and aggregations. Its all in the ManagedObject. I guess they are assuming that the model contents are always data only. Loaded from a webservice that is true - but your example is a good counterexample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.