Skip to content
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

lazy pull request to fix command compatibility with knockout 3.0 #31

Closed
bnemetchek opened this issue Oct 28, 2013 · 1 comment
Closed

Comments

@bnemetchek
Copy link

need to add bindingContext parm as below

// By: Hans Fjällemark and John Papa
// https://github.com/CodeSeven/KoLite

(function (ko) {
ko.command = function (options) {
var
self = function () {
return self.execute.apply(this, arguments);
},
canExecuteDelegate = options.canExecute,
executeDelegate = options.execute;

    self.canExecute = ko.computed(function () {
        return canExecuteDelegate ? canExecuteDelegate() : true;
    });

    self.execute = function (arg1, arg2) {
        // Needed for anchors since they don't support the disabled state
        if (!self.canExecute()) return

        return executeDelegate.apply(this, [arg1, arg2]);
    };

    return self;
};

ko.asyncCommand = function (options) {
    var
        self = function () {
            return self.execute.apply(this, arguments);
        },
        canExecuteDelegate = options.canExecute,
        executeDelegate = options.execute,

        completeCallback = function () {
            self.isExecuting(false);
        };

    self.isExecuting = ko.observable();

    self.canExecute = ko.computed(function () {
        return canExecuteDelegate ? canExecuteDelegate(self.isExecuting()) : !self.isExecuting();
    });

    self.execute = function (arg1, arg2) {
        // Needed for anchors since they don't support the disabled state
        if (!self.canExecute()) return

        var args = []; // Allow for these arguments to be passed on to execute delegate

        if (executeDelegate.length >= 2) {
            args.push(arg1);
        }

        if (executeDelegate.length >= 3) {
            args.push(arg2);
        }

        args.push(completeCallback);
        self.isExecuting(true);
        return executeDelegate.apply(this, args);
    };

    return self;
};

})(ko);

; (function (ko) {
ko.utils.wrapAccessor = function (accessor) {
return function () {
return accessor;
};
};

ko.bindingHandlers.command = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var
            value = valueAccessor(),
            commands = value.execute ? { click: value } : value,

            isBindingHandler = function (handler) {
                return ko.bindingHandlers[handler] !== undefined;
            },

            initBindingHandlers = function () {
                for (var command in commands) {
                    if (!isBindingHandler(command)) {
                        continue;
                    };

                    ko.bindingHandlers[command].init(
                        element,
                        ko.utils.wrapAccessor(commands[command].execute),
                        allBindingsAccessor,
                        viewModel,
                        bindingContext
                    );
                }
            },

            initEventHandlers = function () {
                var events = {};

                for (var command in commands) {
                    if (!isBindingHandler(command)) {
                        events[command] = commands[command].execute;
                    }
                }

                ko.bindingHandlers.event.init(
                    element,
                    ko.utils.wrapAccessor(events),
                    allBindingsAccessor,
                    viewModel,
                    bindingContext);
            };

        initBindingHandlers();
        initEventHandlers();
    },

    update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var commands = valueAccessor();
        var canExecute = commands.canExecute;

        if (!canExecute) {
            for (var command in commands) {
                if (commands[command].canExecute) {
                    canExecute = commands[command].canExecute;
                    break;
                }
            }
        }

        if (!canExecute) {
            return;
        }

        ko.bindingHandlers.enable.update(element, canExecute, allBindingsAccessor, viewModel, bindingContext);
    }
};

})(ko);

@hfjallemark
Copy link

Fixed in #32

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

No branches or pull requests

2 participants