How do you create a KO.JS ViewModel in an external JS file then use it in an html file? This seems like such a simple thing but I cannot get it to work and cannot find any clear information on how to do this. If I have overlooked I apologize and will remove this if someone can point me to the answer.
EXTERNAL vm:
var myApp = (function (myApp) {
myApp.ReportViewModel = function() {
var self = this;
self.test = ko.observable();
}
}(myApp || {}));
Seperate HTML File:
<!DOCTYPE html>
<html>
<head><title>My Page</title></head>
<body>
<table>
<tr>
<td>First Name</td>
<td><input type="text" data-bind='value: test'/></td>
</tr>
</table>
<h2>Hello, <span data-bind="text: test"> </span>!</h2>
<!-- reference this *before* initializing -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>
<script src="myApp.js"></script>
<!-- fire off your app -->
<script>
($function(){
var reportVM = new myApp.ReportViewModel();
ko.applyBindings(reportVM);
});
</script>
EDIT I have made the suggested changes. This is what my project now looks like but it is still not working. Also the knockout.js code is not running at all.
<script type="text/javascript" src="EXTERNAL.js"></script>?