Hi all,
I'm very new to JavaScript, knockout.js etc. I like to go straight to the point:
I want to learn how to use knockout.js with external .js file.
I'm looking forward for your respond.
Here's a basic example I want to make it work:
(Note: When I put the javascript.js code to <script>...</script> inside the HTML file, it works like a charm.)
HTML:
<script type="text/javascript" src="knockout-2.2.1.js"></script>
<script type="text/javascript" src="javascript.js"></script>
...
<h1>Introduction</h1>
<p>First name: <input data-bind="value: firstName" /></p>
<p>Second name: <input data-bind="value: lastName" /></p>
<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>
<p>Full name: <strong data-bind="text: fullName"></strong></p>
<button data-bind="click: capitalizeLastName">Go caps!</button>
...
JAVASCRIPT.JS:
ko.applyBindings(new IntroductionViemModel);
function IntroductionViemModel()
{
this.firstName = ko.observable("Name");
this.lastName = ko.observable("Surname");
this.fullName = ko.computed(function() {return this.firstName() + " " + this.lastName()}, this);
this.capitalizeLastName = function(){var currentVal = this.lastName(); this.lastName(currentVal.toUpperCase());}
}