Skip to content

Update disabled menu item without recreating menu #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hpwdetica opened this issue Jul 11, 2016 · 2 comments
Open

Update disabled menu item without recreating menu #421

hpwdetica opened this issue Jul 11, 2016 · 2 comments
Labels

Comments

@hpwdetica
Copy link

Hi,

I want to be able to change the enabled state of a menu item for an open menu without recreating the menu.

I tried using a custom function for the disabled attribute which returned a variable's value and then changing the variable and calling $(root).contextMenu(). This only caused the update to take effect if I closed and then opened the context menu again.

I see in the demos there is an example of changing whether a menu item is disabled without recreating the menu here: https://swisnl.github.io/jQuery-contextMenu/demo/disabled-changing.html

Some debugging makes it appear to work because when you click the toggle item it will internally call op.update causing the menu to be updated? I have copied this code to achieve the dynamic update ending up with this:

$(function() {
    disabled = true;
    $.contextMenu({
        selector: '.context-menu-one',
        items: {
            "menu item": {
                name: "Menu item",
                disabled: function() {
                    return disabled;
                }
            }
        }
    });

    window.toggleDisabled = function() {
        disabled = !disabled;
        var $el = $('.context-menu-one');
        var contextMenuRoot = $el.data().contextMenu;
        $.contextMenu.op.update.call($el, contextMenuRoot);
    }
});

This appears to work but I'm a bit hesitant diving in to stuff not in the documentation. Is there another way to achieve what I'm looking to do?

Thanks

@bbrala
Copy link
Member

bbrala commented Jul 13, 2016

Interesting way of doing it. Seems like the right way of doing it. Thing is, if i make this public, i'm not sure it won't act strange, especially with the states of input elements.

But if i would make it a public call, it would probably to exactly the same as that. Probably here: https://github.com/swisnl/jQuery-contextMenu/blob/master/src/jquery.contextMenu.js#L1532

So you are using the correct call, but i'm not sure if i should add it as a public call.

@bbrala bbrala added the Feature label Jul 13, 2016
@hpwdetica
Copy link
Author

Ok thanks for the response. I'm going to go with it for now, I'll update here if I run into any problems with this approach. So far it's working ok with simple callback menu items and sub menus. I have not used any input elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants