Skip to content

Add Promise support for main menu #574

Open
@CodeCollector

Description

@CodeCollector

I would like to build my menu items dynamically through an ajax request. This works for sub-menus, but not for the top level menu.

Inside of op.create I was able to make a slight modification to allow this using the same pattern used for handling the sub-menu promise.

  • Wrapped the "each" call in a function.
  • Check if items.then is a function or not
  • In the finish, assign the items to op.items, call the wrapped "each" call, then call show with 'maintain' since its already been called in the first pass with the right position.

I'm not sure if this will have any unintended side effects, but it seems to be working for my use case.

var createCustom = function (opt) {
// create contextMenu items
$.each(opt.items, function (key, item) {
.....all the existing code just wrapped it in a function
};

if ('function' === typeof opt.items.then) {

                    function completedPromise(opt, items) {
                        // Completed promise (dev called promise.resolve). We now have a list of items which can
                        // be used to create the rest of the context menu.
                        if (typeof items === 'undefined') {
                            // Null result, dev should have checked
                            errorPromise(undefined);//own error object
                        }
                        finishPromiseProcess(opt, items);
                    }

                    function errorPromise(opt, errorItem) {
                        // User called promise.reject() with an error item, if not, provide own error item.
                        if (typeof errorItem === 'undefined') {
                            errorItem = {
                                "error": {
                                    name: "No items and no error item",
                                    icon: "context-menu-icon context-menu-icon-quit"
                                }
                            };
                            if (window.console) {
                                (console.error || console.log).call(console, 'When you reject a promise, provide an "items" object, equal to normal sub-menu items');
                            }
                        } else if (typeof errorItem === 'string') {
                            errorItem = { "error": { name: errorItem } };
                        }
                        finishPromiseProcess(opt, errorItem);
                    }

                    function finishPromiseProcess(opt, items) {
                        opt.items = items;
                        createCustom(opt);
                        op.show.call(opt.$trigger, opt, 'maintain', 'maintain');
                    }
                    
                    opt.items.then(completedPromise.bind(this, opt), errorPromise.bind(this, opt));
                }
                else {
                    createCustom(opt);
                }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions