Skip to content

Commit

Permalink
upgrade to Jasmine 2.0, update tests, minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rniemeyer committed Jan 1, 2014
1 parent 0263953 commit 637121e
Show file tree
Hide file tree
Showing 17 changed files with 3,005 additions and 3,237 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-delegatedEvents",
"version": "0.1.2",
"version": "0.1.3",
"main": "build/knockout-delegatedEvents.min.js",
"ignore": [
"examples",
Expand Down
109 changes: 54 additions & 55 deletions build/knockout-delegatedEvents.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-delegatedEvents 0.1.2 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-delegatedEvents 0.1.3 | (c) 2014 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
;(function(factory) {
//CommonJS
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
Expand All @@ -14,72 +14,69 @@
var prefix = "ko_delegated_";
var createDelegatedHandler = function(eventName, root) {
return function(event) {
var data, method, action, owner, matchingParent, command, result,
var data, method, context, action, owner, matchingParent, command, result,
el = event.target || event.srcElement,
context = ko.contextFor(el),
attr = "data-" + eventName,
key = prefix + eventName;

if (context) {
//loop until we either find an action, run out of elements, or hit the root element that has our delegated handler
while (!method && el) {
method = el.getAttribute(attr) || ko.utils.domData.get(el, key);
if (!method) {
el = el !== root ? el.parentNode : null;
}
//loop until we either find an action, run out of elements, or hit the root element that has our delegated handler
while (!method && el) {
method = el.getAttribute(attr) || ko.utils.domData.get(el, key);
if (!method) {
el = el !== root ? el.parentNode : null;
}
}

if (method) {
//get context of the element that actually held the action
context = ko.contextFor(el);

if (context) {
data = context.$data;

if (typeof method === "string") {
//check defined actions
if (method in actions) {
command = actions[method];
if (command) {
action = typeof command === "function" ? command : command.action;
owner = command.owner || data;
}
}
//search for the action
else if (data && data[method] && typeof data[method] === "function") {
action = data[method];
owner = data;
}
if (method) {
//get context of the element that actually held the action
context = ko.contextFor(el);

//search parents for the action
if (!action) {
matchingParent = ko.utils.arrayFirst(context.$parents, function(parent) {
return parent[method] && typeof parent[method] === "function";
});
if (context) {
data = context.$data;

action = matchingParent && matchingParent[method];
owner = matchingParent;
if (typeof method === "string") {
//check defined actions
if (method in actions) {
command = actions[method];
if (command) {
action = typeof command === "function" ? command : command.action;
owner = command.owner || data;
}
}
//a binding handler was used to associate the element with a function
else if (typeof method === "function") {
action = method;
//search for the action
else if (data && data[method] && typeof data[method] === "function") {
action = data[method];
owner = data;
}

//search parents for the action
if (!action) {
matchingParent = ko.utils.arrayFirst(context.$parents, function(parent) {
return parent[method] && typeof parent[method] === "function";
});

action = matchingParent && matchingParent[method];
owner = matchingParent;
}
}
//a binding handler was used to associate the element with a function
else if (typeof method === "function") {
action = method;
owner = data;
}
}

//execute the action as KO normally would
if (action) {
result = action.call(owner, data, event);
//execute the action as KO normally would
if (action) {
result = action.call(owner, data, event);

//prevent default action, if handler returns true
if (result !== true) {
if (event.preventDefault) {
event.preventDefault();
}
else {
event.returnValue = false;
}
//prevent default action, if handler does not return true
if (result !== true) {
if (event.preventDefault) {
event.preventDefault();
}
else {
event.returnValue = false;
}
}
}
Expand All @@ -90,11 +87,13 @@
//create a binding for an event to associate a function with the element
var createDelegatedBinding = function(event) {
var bindingName;
if (event) {
//capitalize first letter
bindingName = "delegated" + event.substr(0, 1).toUpperCase() + event.slice(1);
if (!event) {
return;
}

//capitalize first letter
bindingName = "delegated" + event.substr(0, 1).toUpperCase() + event.slice(1);

//create the binding, if it does not exist
if (!ko.bindingHandlers[bindingName]) {
ko.bindingHandlers[bindingName] = {
Expand Down
4 changes: 2 additions & 2 deletions build/knockout-delegatedEvents.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</div>


<script src="../ext/knockout-2.2.1.js"></script>
<script src="../ext/knockout-3.0.0.js"></script>
<script src="../src/knockout-delegatedEvents.js"></script>
<script>
var Item = function(id, name, price, description) {
Expand Down
Loading

0 comments on commit 637121e

Please sign in to comment.