Based On The First Knockout Tutorial, Why Is My Code Not Running?
I understand how the tutorial works on the page, but I'm trying to set one up locally to create a calculator and can't get knockout.js to work. It doesn't initialize or populate li
Solution 1:
If you're not using jquery, don't load it specially for this. Instead you can activate knockout on window.onload. Example:
Wrap your ko.applyBindings call in a function:
function startKnockout() {
ko.applyBindings(new AppViewModel());
};
Pass the name of your "start" function to window.onload. Note, don't add the () to the function name. This prevents the function executing immediately, and instead ensures it is called as a callback function when the window is loaded.
window.onload = startKnockout;
Solution 2:
You are applying the bindings in a header script tag so there are not yet any elements to bind to at the point that your ko.applyBindings(new AppViewModel()) line runs.
You can provide a callback for the JQuery Window.load function to ensure that everything is properly loaded before the bindings get applied. Example:
<scripttype="text/JavaScript"language="javascript">functionAppViewModel() {
this.firstName = ko.observable("Bert");
this.lastName = ko.observable("Bertington");
}
// Activates knockout.js
$(window).load(function() {
ko.applyBindings(newAppViewModel());
});
</script>
Post a Comment for "Based On The First Knockout Tutorial, Why Is My Code Not Running?"