asp.net mvc 2 - knockout.js load form into viewModel -


i'm using knockout.js handle of data-binding in application. however, on each page load, in document.ready i'm doing initial asnychronous data load this:

$(document).ready() {   getdata() }); 

however, possible instead, load data form (using asp.net mvc2) , reverse load data view model based on data-bind tags?

i feel doesn't work, want confirm i'm not doing improperly.

the 'value' binding sets value of element 1 in view model, no. however, duplicate code 'value' binding own handler set model values values on controls. download debug version of knockout, , ko.bindinghandlers['value'] = { on line 2182. copy binding handler declaration , change 'value' else, add call valueupdatehandler() @ end of init:

ko.bindinghandlers['myvalue'] = {     'init': function (element, valueaccessor, allbindingsaccessor) {         // skipping code         valueupdatehandler(); // update model control values     },     'update': function (element, valueaccessor) {         // skipping code     } }; 

now when use myvalue binding, model updated control values when bound:

<input type="text" data-bind="myvalue: name"></input> 

alternatively call original values instead of copying code, , add code valueupdatehandler after init:

ko.bindinghandlers['myvalue'] = {     'init': function (element, valueaccessor, allbindingsaccessor) {         // call existing value init code         ko.bindinghandlers['value'].init(element, valueaccessor, allbindingsaccessor);          // valueupdatehandler() code         var modelvalue = valueaccessor();         var elementvalue = ko.selectextensions.readvalue(element);         ko.jsonexpressionrewriting.writevaluetoproperty(modelvalue, allbindingsaccessor, 'value', elementvalue, /* checkifdifferent: */ true);     },     'update': function (element, valueaccessor) {         // call existing value update code         ko.bindinghandlers['value'].update(element, valueaccessor);     } }; 

if don't want use ajax, can values javascript serializing model json (razor syntax):

<script type="text/javascript"> var model = @(new htmlstring(new system.web.script.serialization.javascriptserializer().serialize(model))); </script> 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -