Knockout JS Model Inheritance

前端 未结 5 652
一生所求
一生所求 2020-12-23 17:14

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.

5条回答
  •  再見小時候
    2020-12-23 18:12

    I guess you can do something like this:

    var StandardItemModel = function (item, cartItemTypes) {
    var self = this;
    self.standard = new ItemModel(item);
    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);
    }
    

提交回复
热议问题