2

Im using MVC and I am trying to use the introduction tutorials on http://learn.knockoutjs.com/#/?tutorial=intro I would like to implement them in Visual studio though. Im not exactly sure how to do this...

So I create a JS file:

// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {
    this.firstName = "Bert";
    this.lastName = "Bertington";
}

// Activates knockout.js
ko.applyBindings(new AppViewModel());

And my View:

    <!-- This is a *view* - HTML markup that defines the appearance of your UI -->

<p>First name: <strong>todo</strong></p>
<p>Last name: <strong>todo</strong></p>

But I need to create a model and a controller as usual? or just a view and a JS file?

Please advice not sure how this works

John
  • 3,965
  • 21
  • 77
  • 163
  • 4
    You are forgetting to put the `data-bind` attributes in your html. If this is an MVC project, you will still need a controller to serve the page, though you don't need a model. – Kyeotic Apr 23 '13 at 16:34

1 Answers1

0

there are more configs to do if you wish to use them for the true purpose of their design, but here is the basic to make them run the basic:

$(function(){
    var viewModel = 
    {
        firstName : "Bart",
        lastName : "Bartington"
    };
    ko.applyBinding(viewModel);
});

in view :

<p><span data-bind="text: firstName"> <span></p>
<p><span data-bind="text: lastName"> <span></p>

don't forget to forget observables and other setting if you want 2 ways data bindings.

ePezhman
  • 4,010
  • 7
  • 44
  • 80