I am working on knockout js framework and using mapping plugin to update the view model with data that was saved previously as written in the fiddle below.
HTML:
<a href="#" onclick="outputJson();">Click here for output Json</a>
<a href="#" onclick="reloadData();">Click here to load old data</a>
</br>
<input type="text" data-bind="value:firstName" />
<input type="text" data-bind="value:lastName" />
<span data-bind="text:fullName"></span>
Javascript:
var updatedData = [];
var viewModel = {
myviewmodel: ko.observable().extend({noitfy:'always'})
};
function appViewModel() {
var self = this;
self.firstName = ko.observable('Bob');
self.lastName = ko.observable('Smith');
self.fullName = ko.computed(function () {
return self.firstName() + " " + self.lastName();
});
}
viewModel.myviewmodel(new appViewModel());
$(function () {
ko.applyBindings(viewModel.myviewmodel);
});
function outputJson() {
var unmapped = ko.mapping.toJS(viewModel.myviewmodel);
updatedData.push(unmapped);
viewModel.myviewmodel(new appViewModel());
}
function reloadData() {
viewModel.myviewmodel(ko.mapping.fromJS(updatedData[0]));
}
But whenever i use unmap option the viewmodel using ko.mapping plugin the computed observable is not updating.
The scenario is. 1. give first name = 'Bob123', last name = 'Smith' 2. Now click on "Click here for output Json" button, so that the data will be saved temporarly 3. Click on "Click here to load old data" now we can get the old data firstname='Bob123',lastname='Smith', but when i change any of these fields first name or last its not updating the full name.
Can some one tell me how can i make the computed field work on unmap?