akeeseth
akeeseth

Reputation: 845

Can not call .js in html(using knockout)

I am new to knockout using knockout2.1.0. I have an external java script file, but it is not called in my html file. I can not understand.

I have added the following in my html file

  <script src="Scripts/TestJavascript.js"></script>

JS File

///<reference path="~/Scripts/jquery-1.8.1.min.js">
///<reference path="~/Scripts/knockout-2.1.0.debug.js">
$(function AppViewModel() {
this.firstName = ko.observable("rash");
this.lastName = ko.observable("Bertington");
this.fullName = ko.computed(function(){
    return this.firstName() + " " + this.lastName();
}, this);
})
ko.applyBindings(new AppViewModel());

thanks.

Upvotes: 0

Views: 149

Answers (2)

Jason Meckley
Jason Meckley

Reputation: 7591

this code must appear either after the bound html, or within a document ready event (jquery)

function AppViewModel() {
    this.firstName = ko.observable("rash");
    this.lastName = ko.observable("Bertington");
    this.fullName = ko.computed(function(){
        return this.firstName() + " " + this.lastName();
    }, this);
};
ko.applyBindings(new AppViewModel());

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191037

You aren't creating a ViewModel. You are passing it to jquery.

Try

var AppViewModel = function() {
  this.firstName = ko.observable("rash");
  this.lastName = ko.observable("Bertington");
  this.fullName = ko.computed(function(){
      return this.firstName() + " " + this.lastName();
  }, this);
})
ko.applyBindings(new AppViewModel());

Upvotes: 5

Related Questions