Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make click binding accessible via object literal binding or custom bi…

…nding provider (that doesn't use pre-processing)
  • Loading branch information...
commit 8e4e15556d8ad8d3a971d184c5161d5ef6ee17de 1 parent d205f56
@mbest authored
Showing with 24 additions and 0 deletions.
  1. +15 −0 spec/defaultBindingsBehaviors.js
  2. +9 −0 src/binding/defaultBindings.js
View
15 spec/defaultBindingsBehaviors.js
@@ -773,6 +773,21 @@ describe('Binding: Click', {
ko.applyBindings(model, testNode);
ko.utils.triggerEvent(testNode.childNodes[0], "click");
value_of(model.wasCalled).should_be(true);
+ },
+
+ 'Should be able to specifiy click binding using object literal (bypass parsing)': function() {
+ var model = {
+ wasCalled: false,
+ doCall: function (arg1, arg2) {
+ this.wasCalled = true;
+ value_of(arg1).should_be(model);
+ value_of(arg2.type).should_be("click");
+ }
+ };
+ testNode.innerHTML = "<button>hey</button>";
+ ko.applyBindingsToNode(testNode.childNodes[0], {click:model.doCall}, model);
+ ko.utils.triggerEvent(testNode.childNodes[0], "click");
+ value_of(model.wasCalled).should_be(true);
}
});
View
9 src/binding/defaultBindings.js
@@ -5,6 +5,15 @@ ko.utils.arrayForEach(eventHandlersWithShortcuts, function(eventName) {
ko.bindingHandlers[eventName] = {
'preprocess': function(val) {
return { 'key': 'event.' + eventName, 'value': val };
+ },
+ 'flags': bindingFlags_eventHandler,
+ 'init': function(element, valueAccessor, allBindingsAccessor, viewModel) {
+ var newValueAccessor = function () {
+ var result = {};
+ result[eventName] = valueAccessor();
+ return result;
+ };
+ return ko.bindingHandlers['event']['init'].call(this, element, newValueAccessor, allBindingsAccessor, viewModel);
}
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.