knockout.js - Why isn't my knockout observable property, and boud input field being updated? -
i have object observable properties.
this.usertoadd = { id: ko.observable(""), firstname: ko.observable(""), lastname: ko.observable(""), active: ko.observable(""), email: ko.observable(""), roles: ko.observablearray([]) };
i attempt update object when button clicked
$(document).on("click", ".edit", function () { var itemtoupdate = ko.datafor(this); if (itemtoupdate.id !== undefined) { //update observable vm.usertoadd.id = itemtoupdate.id; vm.usertoadd.firstname = itemtoupdate.firstname; vm.usertoadd.lastname = itemtoupdate.lastname; vm.usertoadd.email = itemtoupdate.email; vm.usertoadd.active = itemtoupdate.active; vm.usertoadd.roles = itemtoupdate.roles; //update form, , there must better way $('input[name="id"]').val(itemtoupdate.id); $('input[name="firstname"]').val(itemtoupdate.firstname); $('input[name="lastname"]').val(itemtoupdate.lastname); $('input[name="email"]').val(itemtoupdate.email); $('input[name="active"]').val(itemtoupdate.active); } });
it runs because see input fields being updated values expect, model not updated. doing wrong? also, had use
$('input[name="id"]').val(itemtoupdate.id);
to update input field
<input name="id" type="text" placeholder="id" data-bind="value: id, valueupdate: 'afterkeydown'" />
because value of input field not update when observable property updated. can help?
the ko.observable
call returns function. set value need call new value argument:
vm.usertoadd.id(itemtoupdate.id); vm.usertoadd.firstname(itemtoupdate.firstname); vm.usertoadd.lastname(itemtoupdate.lastname); vm.usertoadd.email(itemtoupdate.email); vm.usertoadd.active(itemtoupdate.active); vm.usertoadd.roles(itemtoupdate.roles);
for further reading: reading , writing observables:
to write new value observable, call observable , pass new value parameter. example, calling
myviewmodel.personname('mary')
change name value 'mary'.