Is it possible to manually insert a computed observable into an array that is generated with the mapping plugin? Here is an example that does not use the mapping plugin, but I would like to know if it could use it.
Lets say I have JSON data from the server:
[{
"OrderID":1,
"FirstName":"Bill",
"LastName":"Smith",
},{
"OrderID":2,
"FirstName":"Jeff",
"LastName":"Evans",
}
{
"OrderID":3,
"FirstName":"Dan",
"LastName":"Johnson",
}]
On my view I have an Order class and a view model:
function Order(order) {
var self = this;
self.OrderID = ko.observable(order.OrderID);
self.FirstName = ko.observable(order.FirstName);
self.LastName = ko.observable(order.LastName);
/*This is what I want to insert after the mapping plugin
generates "orders on the ViewModel*/
self.FullName = ko.computed(function () {
return self.FirstName() + ' ' + self.LastName();
});
}
function ViewModel() {
var self = this;
self.orders = ko.observableArray([])
//Get orders
$.ajax({
url: '@Url.Action("orders")',
type: "post",
success: function (data) {
var mappedOrders = $.map(data, function (item) { return new Order(item) });
self.orders(mappedOrders);
}
})
}
Is it possible to use the mapping plugin to generate the orders array on the view model, and also be able to insert the computed observable "FullName" in the orders array?
After considering the documentation, I successfully applied it to my example as demonstrated in this jsFiddle and in the following lines:
var data = [{
OrderID: '1',
FirstName: 'Bob',
LastName: 'Stevens'
}, {
OrderID: '2',
FirstName: 'Jim',
LastName: 'Johnson'
}];
function order(data) {
var self = this;
var model = ko.mapping.fromJS(data, {}, self);
model.FullName = ko.computed(function () {
return self.FirstName() + ' ' + self.LastName();
});
return model;
}
var mapping = {
create: function (options) {
return new order(options.data);
}
};
function viewModel() {
var self = this;
self.orders = ko.mapping.fromJS(data, mapping);
}
var vm = new viewModel();
ko.applyBindings(vm);
You can, according to the documentation:
var mapping = {
'children': {
create: function(options) {
return new myChildModel(options.data);
}
}
}
var myChildModel = function(data) {
ko.mapping.fromJS(data, {}, this);
this.nameLength = ko.computed(function() {
return this.name().length;
}, this);
}
var viewModel = ko.mapping.fromJS(data, mapping);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With