编辑:原来我的问题是ID10T错误。我复制了一个具体的类定义,但忘了更改名称。 JavaScript很高兴让我重新定义实体,而没有任何与Knockout相关的方法。 D'哦!
这个问题建立在对另一个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...
}
我熟悉自定义映射,但从我能找到的内容来看,它似乎是用于微调子属性而不是修改整个对象。
答案 0 :(得分:0)
如果我在哪里,我会使用真正的原型继承,例如
http://ejohn.org/blog/simple-javascript-inheritance/
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));