Skip to content

Commit

Permalink
Add AMD support in commanding. #25
Browse files Browse the repository at this point in the history
  • Loading branch information
hfjallemark committed Nov 27, 2013
1 parent 0cd0e7c commit d09ef4b
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions knockout.command.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
// By: Hans Fjällemark and John Papa
// https://github.com/CodeSeven/KoLite

(function(ko) {
ko.command = function(options) {
(function (factory) {
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
factory(require("knockout"), exports);
} else if (typeof define === "function" && define["amd"]) {
define(["knockout", "exports"], factory);
} else {
factory(ko, ko);
}
}(function (ko, exports) {
if (typeof (ko) === undefined) {
throw 'Knockout is required, please ensure it is loaded before loading the commanding plug-in';
}

function wrapAccessor(accessor) {
return function () {
return accessor;
};
};

exports.command = function(options) {
var
self = function() {
return self.execute.apply(this, arguments);
Expand All @@ -20,11 +38,11 @@

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

return self;
};

ko.asyncCommand = function(options) {
exports.asyncCommand = function(options) {
var
self = function() {
return self.execute.apply(this, arguments);
Expand All @@ -47,11 +65,11 @@
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);
}
Expand All @@ -63,15 +81,7 @@

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
Expand All @@ -90,7 +100,7 @@

ko.bindingHandlers[command].init(
element,
ko.utils.wrapAccessor(commands[command].execute),
wrapAccessor(commands[command].execute),
allBindingsAccessor,
viewModel,
bindingContext
Expand All @@ -100,16 +110,16 @@

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),
wrapAccessor(events),
allBindingsAccessor,
viewModel,
bindingContext);
Expand All @@ -122,7 +132,7 @@
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) {
Expand All @@ -131,12 +141,12 @@
}
}
}

if (!canExecute) {
return;
}

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

0 comments on commit d09ef4b

Please sign in to comment.