lemaiwo / SAPUIRightClickTutorial

0 stars 2 forks source link

Duplicate Menus Created #1

Open mitch-b opened 9 years ago

mitch-b commented 9 years ago

Hi,

Thanks for the tutorials. I have a question on generating this menu multiple times, with the code issue line being in the Main.controller.js:44.

First, I have swapped the sap.ui.commons Menu with the sap.ui.unified Menu as suggested by documentation. Secondly, when you trigger the menu a second time, we are prompted with an error from the UI5 core, since multiple menus have been created with the same ID. What is the best way to handle this scenario?

ERROR: adding element with duplicate id 'menu1-1'
mitch-b commented 9 years ago

Okay, easy enough.

    rightPress: function(evt) {
        //basic menu
        if (!this._menu) {
            this._menu = new sap.ui.unified.Menu("menu1-1", {ariaDescription: "File Menu"});
            var oMenuItem13 = new sap.ui.unified.MenuItem("item1-1-3",{text: "Add sales order"});
            this._menu.addItem(oMenuItem13);
            var oMenuItem14 = new sap.ui.unified.MenuItem("item1-1-4",{text: "A/R invoice"});
            this._menu.addItem(oMenuItem14);
            this.getView().addDependent(this._menu);
        }

        var eDock = sap.ui.core.Popup.Dock;

        this._menu.open(false,
                evt.oSource.getFocusDomRef(),
                eDock.BeginTop,
                eDock.BeginBottom,
                evt.oSource.getDomRef());
    }

If you are going to use sap.ui.unified controls, make sure your UI5 bootstrapping script has the library included:

    <script src="https://sapui5.hana.ondemand.com/sdk/resources/sap-ui-core.js"
            [ ... ]
            data-sap-ui-libs="sap.m,sap.ui.commons,sap.ui.unified"
            [ ... ]
    >
    </script>

Feel free to close this issue if you'd like - hopefully this helps someone else.