Just getting started - 'ko' is undefined

2,623 views
Skip to first unread message

Daniel Williams

unread,
Dec 8, 2010, 1:15:06 AM12/8/10
to KnockoutJS
I have a MVC 2.0 project, and am trying to get knockout working in it.
In Site.Master I have the following in the header:


<script type='text/javascript' src='~/scripts/jquery-1.4.2.js'></
script>
<script type='text/javascript' src='~/scripts/jquery.tmpl.js'></
script>
<script type='text/javascript' src='~/scripts/
knockout-1.1.1.debug.js'></script>

I have also added this in the Site.Master:

<asp:ContentPlaceHolder ID="bottom" runat="server" />


In my view, I have the following:


<asp:Content ID="foo" ContentPlaceHolderID="bottom" runat="server">
<script type="text/javascript">

debugger;
var initialData = <%= new
JavaScriptSerializer().Serialize(Model) %>;
var viewModel = {
gifts : ko.observableArray(initialData)
};
ko.applyBindings(document.body, viewModel);
</script>

</asp:Content>

When I run, I get "Microsoft JScrpt error: 'ko' is undefined"

Whereis ko created? How can I get the order right? I feel this is a
very basic piece that I am missing, but it prevents me from getting
anywhere with knockout.

Daniel Williams

unread,
Dec 8, 2010, 1:35:34 AM12/8/10
to KnockoutJS

Aha - found the problem. My js files were not being found. Darned
confusing paths! Here is how to point to the script files:

<script type='text/javascript' src='<%= Url.Content("~/scripts/
jquery-1.4.2.js") %>' ></script>
<script type='text/javascript' src='<%= Url.Content("~/scripts/
jquery.tmpl.js") %>' ></script>
<script type='text/javascript' src='<%= Url.Content("~/scripts/
knockout-1.1.1.debug.js") %>' ></script>

Daniel Williams

unread,
Dec 8, 2010, 1:40:11 AM12/8/10
to KnockoutJS

Also, this line from the example on knockoutjs.com website is not
correct:

ko.applyBindings(document.body, viewModel);

What works is either:

ko.applyBindings( viewModel, document.body);

or

ko.applyBindings( viewModel);

But now I'm cooking - this is fun!




On Dec 7, 11:15 pm, Daniel Williams <dlw...@gmail.com> wrote:

Steven Sanderson

unread,
Dec 8, 2010, 3:40:19 PM12/8/10
to knockoutjs
ko.applyBindings(document.body, viewModel)

Hi Daniel

You're right, ko.applyBindings(document.body, viewModel) would not be correct - the parameters go the other way around. But where on knockoutjs.com did you find this error? I've searched and can't see it. Please let me know where it is so I can fix it :)

Thanks
Steve

Daniel Williams

unread,
Dec 8, 2010, 5:25:22 PM12/8/10
to knock...@googlegroups.com
Steven,

Here is where I found the code:

http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/

This morning I also saw that this is old syntax, so my bad for not
researching it well enough. I was just too excited to stop and read
more.
The reason I'm doing this is that last night while out for a walk I
listened to your podcast interview with Scott Hanselman and just had
to try it out.

Cheers,
Daniel Williams

Reply all
Reply to author
Forward
0 new messages