Created
January 21, 2014 07:46
-
-
Save IcodeNet/8535858 to your computer and use it in GitHub Desktop.
binding-dump.js will dump the current Knockoutjs model
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
// Usage in HTML | |
// <div data-bind="dump: $data"></div> | |
// <div data-bind="dump: $root.results"></div> (if results was an observable/observableArray) | |
// <div data-bind="dump: $root.results()[0].someProperty"></div> (first items property) | |
ko.bindingHandlers.dump = { | |
init: function (element, valueAccessor, allBindingsAccessor, viewmodel, bindingContext) { | |
var context = valueAccessor(); | |
var allBindings = allBindingsAccessor(); | |
var pre = document.createElement('pre'); | |
element.appendChild(pre); | |
var dumpJSON = ko.computed({ | |
read: function () { | |
var en = allBindings.enabled === undefined || allBindings.enabled; | |
return en ? ko.toJSON(valueAccessor(), null, 2) : ''; | |
}, | |
disposeWhenNodeIsRemoved: element | |
}); | |
ko.applyBindingsToNode(pre, | |
{ | |
text: dumpJSON, | |
visible: dumpJSON | |
} | |
); | |
return { controlsDescendentBindings: true }; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment