Created
December 6, 2017 15:32
-
-
Save codenamejason/cb4380f3a4a3c4183170ad18c4cab900 to your computer and use it in GitHub Desktop.
Knockout Log/Logger Binding Handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//- for testing (logchange) | |
ko.extenders.logChange = function (target, option) { | |
target.subscribe(function (newValue) { | |
console.log(option + " : " + newValue); | |
}); | |
return target; | |
}; | |
ko.bindingHandlers.logger = { | |
update: function (element, valueAccessor, allBindings) { | |
//store a counter with this element | |
var count = ko.utils.domData.get(element, "_ko_logger") || 0, | |
data = ko.toJS(valueAccessor() || allBindings()); | |
ko.utils.domData.set(element, "_ko_logger", ++count); | |
if (window.console && console.log) { | |
console.log(count, element, data); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment