I try to use Knockout like WPF-MVVM
.
In WPF
there is an option to notify the VM on property change.
For example: if I have a textBox, I can use UpdateSourceTrigger=PropertyChanged
for notify the VM every click.
<Grid DataContext="MyVM">
<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</Grid>
In Knockout I have build this simple app:
VM:
function AppViewModel() {
this.firstName = ko.observable("Bert");
this.lastName = ko.observable("Bertington");
this.fullName = ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this);
}
ko.applyBindings(new AppViewModel());
View:
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<p>Full name: <strong data-bind="text: fullName"></strong></p>
I'm trying without success, to update fullName
on textBox input click (not onblur).
EDIT:
The code works fine. I just want that lastname will update while writing on the firstname/lastname textbox.
Now it's updated onblur, it's not updated while writing.