I am fairly new to knockout js and the documentation maintained on official site does not provide complete html file. So I ended up writing my own script tags. When I created the html markups it did not work as expected. However just altering the position of script tags makes it work.
My question is, when the script tag is placed up top, then the function is made available before it is called in the html body elements.Still it does not data bind, whereas placing the script at bottom just solves it. How is this possible by just reordering the position of script tag? Can someone provide the order in which things must be made available. I understand that many people prefer having script towards the end so that the DOM is completely ready and for speed considerations.
My code below:
- Does not data bind.
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
</head>
<body>
<script type="text/javascript">
var myViewModel = {personName:'Bob', personAge:31};
ko.applyBindings(myViewModel);
</script>
The name is <span data-bind="text: personName"></span>
</body>
</html>
- Works
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
</head>
<body>
The name is <span data-bind="text: personName"></span>
<script type="text/javascript">
var myViewModel = {personName:'Bob', personAge:31};
ko.applyBindings(myViewModel);
</script>
</body>
</html>