1

编辑:原来我的问题是 ID10T 错误。我复制了一个具体的类定义,但忘记更改名称。JavaScript 很高兴地让我重新定义实体,而无需任何与 Knockout 相关的方法。哦!

这个问题建立在对另一个Knockout/inheritance question的回答之上。使用该问题的答案,我能够建立一个基本的层次结构。但是,我想像通常使用对象数据一样使用映射插件。但是,当我尝试使用映射时,我的淘汰子类的行为并不像它应该的那样。

这是代码的一部分:

tubs.Gen2Event = function (data) {
    var self = this;
    //...Set a bunch of props...
    return self;
}

tubs.Gen2LandedEvent = function (data) {
    var self = this;
    ko.utils.extend(self, new tubs.Gen2Event(data));
    // If I exclude the following mapping call, the object is fine
    ko.mapping.fromJS(data, {}, self);
    //...Other methods that worked fine before mapping...
}

我熟悉自定义映射,但据我所知,它似乎是为了微调子属性,而不是修改整个对象。

4

1 回答 1

0

例如,如果我在哪里,我会使用真正的原型继承

http://ejohn.org/blog/simple-javascript-inheritance/

http://jsfiddle.net/4Kp3Q/

Person = Class.extend({
  init: function(data){
      this.firstname = ko.observable();
      this.lastname = ko.observable();      
      ko.mapping.fromJS(data, {}, this);
  }
});

Employee = Person.extend({
  init: function(data){
      this.salary = ko.observable();     
      this._super(data);
  }
});

var data = { firstname: "foo", lastname: "bar", salary: 200000 };
ko.applyBindings(new Employee(data));
于 2013-04-26T07:20:38.460 回答