I have three relatively similar knockout models in my application and I would like to extend a base model to combine common properties rather than repeat myself three times.
example
var ItemModel = function (item) { var self = this; self.order = ko.observable(item.order); self.title = ko.observable(item.title); self.price = ko.observable(item.price); self.type = ko.observable(item.type); }; var StandardItemModel = function (item, cartItemTypes) { var self = this; self.order = ko.observable(item.order); self.title = ko.observable(item.title); self.price = ko.observable(item.price); self.type = ko.observable(item.type); self.isInCart = ko.computed(function () { return cartItemTypes().indexOf(item.type) > -1; }, self); self.itemClass = ko.computed(function () { return self.isInCart() ? "icon-check" : "icon-check-empty"; }, self); }; var CustomItemModel = function (item) { var self = this; self.order = ko.observable(item.order); self.title = ko.observable(item.title); self.price = ko.observable(item.price); self.type = ko.observable(item.type); self.icon = item.icon; };
I would like to use ItemModel as a base class and just add the extra properties as necessary.
I think you can use ko.utils.extend like this
ko.utils.extend(self, new ItemModel(item));
inside the StandardItemModel
like this: http://jsfiddle.net/marceloandrader/bhEQ6/
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