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","a.tester@example.com");

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

不幸的是,推送到我的数组的所有值都是空的。谁能给我一个提示,这里有什么问题?

4

3 回答 3

8

如果所有值都是可观察的,那么您不需要在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", 'aaaa@abc.cde');
>> ko.toJS(person) 
{
    id : 1,
    name : "Name",
    loginName : "LoginName",
    email : "aaaa@abc.cde"
} 
于 2013-01-07T11:27:57.020 回答
1

您应该为该人的可观察操作设置值

person().id = id;
person().name = name;
person().loginName = login;
person().email = email;
于 2013-01-07T11:22:07.857 回答
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);
}

变量之前被设置为私有,没有注意到它。谢谢大家的帮助。

于 2013-01-07T14:33:24.780 回答