Applying a knockout to a completed form without wiping the form data due to an empty viewmodel?

I have a script that initializes a knockout by applying an empty view model to my form.

When the user enters data on the form, the view model is updated accordingly - this is the expected behavior.

Javascript

var viewModel = {
  myField : ko.observable(),
  init : function (somedata) {
    ...
    ko.applyBindings(this, container);
  },
  ...more stuff...
}

Partial

<%:Html.TextBoxFor(x => x.MyField, new Dictionary<string, object> { { "data-bind", "value: myField, valueUpdate: 'keyup'" } })%>

When I call my javascript to apply the knockout in the existing form, the value already in MyField(and then in my input field) is wiped with empty data in my knockout model (this view model is part of my javascript located in another file).

, MVC , MyField MyField. init, view MyField, , JavaScript.

?

+3
2

- :

1- ( ), javascript , .

: http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/. , :

<script type="text/javascript"> 
    var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>; 
</script>

Data applyBindings , , javascript, .

2- ( , / ), viewModel , applyBindings. , , .

, , , javascript .

+1

, , ko.observable .

0

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


All Articles