Is it possible to “insert” updates into the night view ViewModel?

I'm currently looking for a solution to my problem: our ASP.NET MVC application has pages that are used to visualize real-time data from industrial devices. When the page loads, the loading icon is displayed when I retrieve the viewmodel data with the current values ​​for all the data from the database. This works pretty well, but it is static, by which I mean that the values ​​do not change on the page after the download is complete. The web application itself uses a TCP listener that receives messages with values ​​from devices. These messages (which mainly consist of a device identifier, a data point identifier and a value) do not arrive at fixed intervals, but are based on events, for example. when the temperature value changes by 0.5 K up or down.

On my page, I have graphical widgets, such as sensors and many other elements that correctly display the values ​​from the source data that are loaded when the page loads. They are tied to the Knockout view model.

The problem is this: whenever a new value arrives at the server, I want to display it on the page without having to reload. I definitely don’t want to retransmit the entire view model with several hundred data points per message that arrives at the server (approximately 1 to 15 per second). For this, I implemented a SignalR framework that really works great. With this mechanism, I now get a new value in the client window (this means that I get it in Javascript and now have a value object, as described below).

Now I need the following: since each view model is created dynamically, they are all different. The tree of objects and properties is not the same for two devices, so each of them can have different levels of subobjects. The only thing that’s the same is the structure of the object that actually stores the value for each datapoint: it always consists of the above device identifier, datapoint identifier and value.

I need a way to update the double-type value inside a value object within the view model, the device identifier and dataset identifier correspond to the newly received value message (which also consists of these two address and value identifiers).

, . ? ? Knockout-MVC ( kMVC nuget), "" Knockout.js , .

!

+4
2

http://nthdegree.azurewebsites.net/mvvm-2/

,

, , .

, , ajax (. ).

ko.mapping.fromJS(newData, mapping, modelToUpdate)


var mapping = {}; //define your mapping (see documentation or blog post)

function ViewModel(){
    var self = this; 

    self.model = ko.mapping.fromJS({}, mapping);
    self.hub = $.connection.myHub(); 
    self.hub.client.updateModel = function(data){
       ko.mapping.fromJS(data, mapping, self.model);
    };
    self.hub.start().done(function(){
       //you could either make a call or have the "OnConnected" method trigger an 'updateModel'
    });
}
+1

ViewModel DOM , KO. " " ( , ), , , , ?

; - KO foreach .

+1

Source: https://habr.com/ru/post/1537563/


All Articles