Skip to content

Instantly share code, notes, and snippets.

@matchdav
Last active August 29, 2015 14:03
Show Gist options
  • Save matchdav/45cc797b8303f1f6ab16 to your computer and use it in GitHub Desktop.
Save matchdav/45cc797b8303f1f6ab16 to your computer and use it in GitHub Desktop.
How to defer a knockout binding.
<html>
<head>
<title></title>
</head>
<body>
<div id="bindme">
<p data-bind="text:prop"></p>
</div>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script>
<script type="text/javascript">
function TestView(prop) {
this.loaded = ko.observable(false);
this.prop = ko.observable(prop);
}
var tv = new TestView('Hi there');
alert(tv.prop());
alert(tv.loaded());
tv.loaded.subscribe(function(isLoaded){
if(isLoaded) ko.applyBindings(tv,document.getElementById('bindme'));
});
setTimeout(function(){
tv.loaded(true)
}, 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment