Permalink
Browse files

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

…nding provider (that doesn't use pre-processing)
  • Loading branch information...
1 parent d205f56 commit 8e4e15556d8ad8d3a971d184c5161d5ef6ee17de @mbest committed Mar 29, 2012
Showing with 24 additions and 0 deletions.
  1. +15 −0 spec/defaultBindingsBehaviors.js
  2. +9 −0 src/binding/defaultBindings.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);
}
});
@@ -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);
}
}
});

0 comments on commit 8e4e155

Please sign in to comment.