2013-01-07 114 views
3

我有下面的代碼片段淘汰賽解開復雜的對象

function Person() { 
    var id; 
    var name; 
    var loginName; 
    var email; 
} 

function Substitude() { 
    this.substitude = ko.observable(new Person()); 
    this.subBegin = ko.observable(Date()); 
    this.subEnd = ko.observable(Date()); 
} 

function SampleSubstitude() { 
    var testing = ko.observable(new Substitude()); 
    var tester = getPerson(88,"Alpha Tester","a.tester","[email protected]"); 

    testing.substitude = tester; 

    return ko.utils.unwrapObservable(testing); 
} 

function getPerson(id, name, login, email) { 
    var person = ko.observable(new Person()); 
    person.id = id; 
    person.name = name; 
    person.loginName = login; 
    person.email = email; 

    return ko.utils.unwrapObservable(person); 

} 

這是我的看法型號:

function AbsenceRequestModel() { 
    this.delegations = ko.observableArray(); 
    this.addsubstitudeclick = function() { 
     var raw = SampleSubstitude(); 
     var obj = ko.utils.unwrapObservable(new raw()); 
     this.delegations.push(obj); 
    } 
}; 

不幸的是所有的值推到我的數組是空的。任何人都可以給我一個提示,這裏有什麼不對?

+0

它是替代品,而不是替代品。這更像是一種語法的東西,但我不得不說出來。 – arkanciscan

+0

@arkanciscan:這是更多*拼寫*的東西。與語法無關。 ;-) –

回答

0

問題解決了:

function Person(id, name, login, email) { 
    this.id = ko.observable(id); 
    this.name = ko.observable(name); 
    this.loginName = ko.observable(login); 
    this.email = ko.observable(email); 
} 

變量之前私人設置,沒我沒有注意到它。 感謝您的幫助。

1

你應該值設置爲觀察到運的人

person().id = id; 
person().name = name; 
person().loginName = login; 
person().email = email; 
6

如果所有的值被觀察到的,那麼你就不需要你的Person構造函數和你的對象之間的代理方法:

function Person(id, name, login, email) { 
    var id = ko.observable(id); 
    var name = ko.observable(name); 
    var loginName = ko.observable(login); 
    var email = ko.observable(email); 
} 

var person = new Person(1, "Name", "LoginName", '[email protected]'); 
>> ko.toJS(person) 
{ 
    id : 1, 
    name : "Name", 
    loginName : "LoginName", 
    email : "[email protected]" 
}