我有以下代码段
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);
}
};
不幸的是,推送到我的数组的所有值都是空的。任何人都可以给我一个提示,这里有什么不对吗?
答案 0 :(得分:7)
如果所有值都是可观察的,那么您的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"
}
答案 1 :(得分:1)
您应该将值设置为observable op the person
person().id = id;
person().name = name;
person().loginName = login;
person().email = email;
答案 2 :(得分: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);
}
变量之前设置为私有,没有注意到它。 谢谢大家的帮助。