2013-04-25 75 views
1

編輯:原來我的問題是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

我會用真正的原型繼承,如果我在那裏你,例如

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));