Yoong Kim
Yoong Kim

Reputation: 310

javascript - knockout - how it works?

I am trying to use knockout and I wonder myself how it works REALLY from a network point of view : where can I see the "calls" from the browser client to the server to retrieve data? I mean : I used to use Ajax calls to populate forms, tables... => we can see the ajax calls to the server. Same thing when I need to submit a form : I can see the ajax calls. That means I can debug with Firefbug and see the parameters sent/ the response received, inluding the headers (request/response). With knocknout, the data in the form are "binding" automatically by the framework ko. So, does someone know how it works really? how the calls are done? is there any way to "see" the data flow?

Upvotes: 0

Views: 105

Answers (2)

kidwon
kidwon

Reputation: 4524

It's based on the publish-subscribe pattern. Whenever something is changed it notifies about it.

Here's some info about it http://msdn.microsoft.com/en-us/magazine/hh201955.aspx

Upvotes: 1

Paul Alan Taylor
Paul Alan Taylor

Reputation: 10680

From a network point of view, nothing changes when using knockout. You'll still need to make AJAX calls to populate your view models, but they're outside the framework, not part of it. This means that you can still put a breakpoint on your AJAX calls and observe the stuff being sent and received.

A major code departure is that your network calls will probably now exist within a knockout viewmodel.

var someVm = function(data) {
  var self = this;
  self.Id = ko.observable(data.Id);
  // ...

  self.getItems = function() {
     // AJAX call here, now method on a vm
  }
}

However, as TJ Crowder points out - the key mechanic of knockout is binding a client side view model to a user interface, either for data population or visibility control in a single page application. Networking is something you'll have to handle, but it's not part of knockout. Most likely, you'll make small changes in your placement of AJAX calls.

Upvotes: 1

Related Questions