0

I am new to MVVM , and i wanted to understand , if you have a model in the back end, say a c# library which is getting data from the database or any other service. how would the Model notify the viewmodel. i understand in the MVVM INotifyprovier does that for WPF (just read it somewhere), but how about Web app based scenario, does Viewmodel in java-script always have to ping Model to identify if there is a change in the model and then propagate to UI. I am assuming that the viewmodel would always have to send Ajax request to a asmx or api with a set interval of 10 minutes or so (just an example). is that how it works end to end. any example will be great.

Because one of the properties is, automatic UI refresh, so I am assuming that fresh data needs to be asked from server at regular intervals. A lot of examples i see on the web is only interacting between ViewModel and View, but hardly i see any thing with Model, ViewModel and View all combined together.

I could think that if one uses SignalR which sends a ping from Server to the client, then the Viewmodel could be updated and hence the View.

But if every time you need to ask a fresh set of data from Sever, so what's with the hype of Knockout, Jquery has been doing that for a while, except the Declarative binding stuff, in the knockout library.

Appreciate if somebody could correct me.

Thanks

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
Rahul
  • 95
  • 1
  • 2
  • 13
  • If you work through [the KnockoutJS tutorial](http://learn.knockoutjs.com/) and/or read up on the [relevant documentation](http://knockoutjs.com/documentation/json-data.html) things should become pretty clear. In addition you may want to read up on [WebSockets](https://developer.mozilla.org/en-US/docs/WebSockets) and its fallbacks/alternatives. – Jeroen Feb 11 '13 at 23:49
  • Thanks Jeroen, i did , but had this question of how everything fits together. I updated my question with more comments into it. So per the links that you sent, the Model is infact the backend, right? For MVVM in javascript (knockout) here is only ViewModel and View. ViewModel is the one who sends the request to the server, correct? – Rahul Feb 11 '13 at 23:54

1 Answers1

1

It is up to your client application to fetch new data since server doesn't have any concept of observables. There is also a useful mapping plugin that could automatically map your javascript data from the server into an observable. That way when you fetch data you don't have to re-map your data.

However, it is possible to notify client of changes from server. One way is to make server push changes to the client is via HTML5 WebSockets. SignalR is a good library candidate for that task. It that would open a WebSockets connections so server can notify client of changes. And you would use Service Broker SqlDependency to trigger event notifications on updates. An example could be found here.

Good luck!

Also here is a really good video to watch about knockoutjs that would give you understanding of the framework.

Sergey
  • 3,214
  • 5
  • 34
  • 47
  • Thanks, But where is the Model over here, Is model being referred to the server. No documentation on Model in knockoutjs.com and Ideally how would you do it, if not using websockets and signalR,Will you always send request to the server using the code in ViewModel to ask for fresh data on a regular interval of time(as explained in the question) Just curious. – Rahul Feb 12 '13 at 00:04
  • 1
    Here is the explanation on the model :) http://knockoutjs.com/documentation/observables.html#mvvm_and_view_models. And yes you would need to poll data at intervals without WebSockets. – Sergey Feb 12 '13 at 00:15