Asp.net-mvc – ASP.Net Web Api + KnockoutJs + MVC4 – Tying it together

asp.net-mvcasp.net-web-apiknockout-mapping-pluginknockout.js

I am starting a new project, and keen to make use of the KnockoutJS + Web Api which are new to me, I have a good understanding of the Web Api, but Knockout is tough to get my head around at the moment.

This is my initial thoughts of how I want my app to work:

  • I have a standard MVC controller such as LeadsController
  • LeadsController has an Action called ListLeads, this doesn't actually return any data though, but just returns a view with a template to display data from Knockout.
  • The ListLeads view calls my api controller LeadsApiController via ajax to get a list of leads to display
  • The leads data is then mapped to a KnockoutJs ViewModel (I don't want to replicate my view models from server side into JavaScript view models)
  • I want to use external JavaScript files as much as possible rather than bloating my HTML page full of JavaScript.

I have seen lots of examples but most of them return some initial data on the first page load, rather than via an ajax call.

So my question is, how would create my JavaScript viewModel for Knockout when retrieved from ajax, where the ajax url is created using Url.Content().

Also, what if I need additional computed values on this ViewModel, how would I extend the mapped view model from server side.

If I haven't explained myself well, please let me know what your not sure of and I'll try and update my question to be more explicit.

Best Answer

I think your design is a good idea. In fact, I am developing an application using exactly this design right now!

You don't have to embed the initial data in your page. Instead, when your page loads, create an empty view model, call ko.applyBindings, then start an AJAX call which will populate the view model when it completes:

$(function () {
    var viewModel = {
        leads: ko.observableArray([]) // empty array for now
    };

    ko.applyBindings(viewModel);

    $.getJSON("/api/Leads", function (data) {
        var newLeads = ko.mapping.fromJS(data)(); // convert to view model objects
        viewModel.leads(newLeads); // replace the empty array with a populated one
    });
});

You'll want to put a "Loading" message somewhere on your page until the AJAX call completes.

To generate the "/api/Leads" URL, use Url.RouteUrl:

<script>
    var apiUrl = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "Leads" })';
</script>

(That's assuming your API route configured in Global.asax or App_Start\RouteConfig.cs is named "DefaultApi".)

The knockout mapping plugin is used above to convert the AJAX JSON result into a knockout view model. By default, the generated view model will have one observable property for each property in the JSON. To customise this, such as to add additional computed properties, use the knockout mapping plugin's "create" callback.

After getting this far in my application, I found I wanted more meta-data from the server-side view models available to the client-side code, such as which properties are required, and what validations are on each property. In the knockout mapping "create" callbacks, I wanted this information in order to automatically generate additional properties and computed observables in the view models. So, on the server side, I used some MVC framework classes and reflection to inspect the view models and generate some meta-data as JavaScript which gets embeded into the relevant views. On the client side, I have external JavaScript files which hook up the knockout mapping callbacks and generate view models according the meta-data provided in the page. My advice is to start out by writing the knockout view model customisations and other JavaScript by hand in each view, then as you refactor, move generic JavaScript functions out into external files. Each view should end up with only the minimal JavaScript that is specific to that view, at which point you can consider writing some C# to generate that JavaScript from your server-side view model annotations.