javascript - Knockout with not creating property -


i have little problem. fiddle

http://jsfiddle.net/lkqtu/25524/

js

var hellovm = function() {     var vm=this;     vm.obj1=ko.observable({});     vm.obj2=ko.observable({});     vm.test = function()     {         alert("fffff");         alert("obj 1 name "+vm.obj1().name);          alert("obj 2 name "+vm.obj2().name);      };  }; ko.applybindings(new hellovm(), document.getelementbyid('foo')); 

html

<div id="foo">     <div>         <input type="text" data-bind="value:obj1().name" />     </div>     <div data-bind="with:obj2()" style="margin-top:2px">         <input type="text" data-bind="value:name" />     </div>     <button data-bind="click:test">test</button> </div> 

first case binding unexisting property of object directly , second case binding unexisting property of object via "with" binding

in second case(using "with") knockout doesn't create property in object. it'll created in first case.

what kind of problem have?

here jsfiddle

i hope after.

html

<div id="foo">     <div>         <input type="text" data-bind="value: obj1().name" />     </div>     <div data-bind="with:obj2()" style="margin-top:2px">         <input type="text" data-bind="value:name" />     </div>     <button data-bind="click:test">test</button> </div> 

js

the property name not exist , therefore can not assign value it. below have set observables have object name property.

var hellovm = function () {     var vm = this;     // added object property of name     vm.obj1 = ko.observable({         name: ''     });     // added object property of name = result     vm.obj2 = ko.observable({         name: 'result'     });     vm.test = function () {         alert("fffff");         alert("obj 1 name " + vm.obj1().name);         alert("obj 2 name " + vm.obj2().name);     };  }; ko.applybindings(new hellovm(), document.getelementbyid('foo')); 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -