Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing knockout.js I need to update my model from a server call then show this on the view
    primarykey
    data
    text
    <p>I am using MVC 4.0 with knockout.js to create a page that will call to a database every 5-10 seconds, and update the model with the new object retrieved from the database, then update the view with the new model. </p> <p>Currently I am making the initial JSON get call and this is returning the model, and I am then able to bind this to the view. I then set up the getJSON call again to do the update every 10 seconds, and it will execute the function and get back the data, but no update is seen on the screen. I have tried calling the ko.applyBindings after each call, but there is still no update on the page, then it crashes the browser after a few calls.</p> <p>I'm not sure what exactly I'm doing wrong. I have included the relevant parts of the code below.</p> <p>JavaScript:</p> <pre><code>&lt;script type="text/javascript"&gt; var viewModel; var update = setInterval(function () { $.getJSON( "/Home/Get", {}, function (model) { viewModel = model; bindViewModel(viewModel); }); }, 10000); $(document).ready( function () { $.getJSON( "/Home/Get", {}, function (model) { viewModel = model; bindViewModel(viewModel); }); }); function bindViewModel(viewModel) { ko.applyBindings(viewModel); } &lt;/script&gt; </code></pre> <p>Table to Display Model </p> <pre><code>&lt;div data-bind="foreach: { data: Events, as: 'evnts' }"&gt; &lt;span data-bind="text: evnts.LastUpdate"&gt;&lt;/span&gt; &lt;table id="EventsTable" style="display:inline; float:left;"&gt; &lt;tbody&gt; &lt;tr id="EvntsHeader" data-bind="visible: evnts.IsVisible"&gt; &lt;td&gt; &lt;span data-bind="text: evnts.Name"&gt;&lt;/span&gt; &lt;/td&gt; &lt;tr&gt; &lt;td&gt; &lt;table id="MarketsTable"&gt; &lt;tbody data-bind="foreach: { data: evnts.Markets, as: 'markets' }"&gt; &lt;tr&gt; &lt;td id="MarketHeader"&gt; &lt;span data-bind="text: markets.Name"&gt;&lt;/span&gt; &lt;span data-bind="text: markets.NumberOfRunners"&gt;&lt;/span&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;table id="SelectionTable"&gt; &lt;tbody data-bind="foreach: { data: markets.Selections, as: 'selections' }"&gt; &lt;tr id="Selections"&gt; &lt;td&gt;&lt;span data-bind="text: selections.Number, visible: selections.IsVisible"&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span data-bind="text: selections.Name, visible: selections.IsVisible"&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span data-bind="text: selections.CurrentPrice, visible: selections.IsVisible"&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span data-bind="text: selections.OpeningPrice, visible: selections.IsVisible"&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p></p> <p>Controlller - JSON Get</p> <pre><code>[AcceptVerbs(HttpVerbs.Get)] public JsonResult Get() { var model = new TestModel {LastUpdate = DateTime.Now}; if (TestConstants.Update == false) { GetModelFirstTime(model); TestConstants.Update = true; } else { UpdateModel(model, DateTime.Now); } return Json(model, JsonRequestBehavior.AllowGet); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload