knockout "with" binding not working properly

1.4k views Asked by At
   <div data-bind="with: project"> 
     <div>
       <label>Name</label>
       <input class="inputControl" data-bind="value: name"/>
     </div>
     <div>
        <label>Id</label>
        <input class="inputControl" data-bind="value: id"/>
      </div>
   </div> 

In this code, 'project' is an observable variable and it has 2 properties i.e. name and id. 'name' and 'id' fields are updating according to 'project' observable variable. but when we redirect to other page and come back on same page, then 'name' and 'id' fields are not updating even 'project' is updating. Please let us know for any suggestions. Thanks in advance.

1

There are 1 answers

1
alexmac On

To use name and id values in view, they are should be observables.

For example:

function projectViewModel() {
    var self = this;
    self.name = ko.observable();
    self.id= ko.observable();
}

function vm() {
    var self = this;
    self.project = ko.observable(new projectViewModel());
}