编辑:原来我的问题是一个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...
}我熟悉自定义映射,但从我所能找到的信息来看,它似乎是用于微调子属性,而不是修改整个对象。
发布于 2013-04-26 15:20:38
我会使用真正的原型继承,如果我在你那里,例如
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));https://stackoverflow.com/questions/16224941
复制相似问题