Last active
August 29, 2015 14:03
-
-
Save matchdav/45cc797b8303f1f6ab16 to your computer and use it in GitHub Desktop.
How to defer a knockout binding.
This file contains 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
<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