I've got the following code snippet
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);
}
This is my View Model:
function AbsenceRequestModel() {
this.delegations = ko.observableArray();
this.addsubstitudeclick = function () {
var raw = SampleSubstitude();
var obj = ko.utils.unwrapObservable(new raw());
this.delegations.push(obj);
}
};
Unfortunately all values pushed to my array are empty. Can anybody give me a hint, what's wrong about here?
I've got the following code snippet
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);
}
This is my View Model:
function AbsenceRequestModel() {
this.delegations = ko.observableArray();
this.addsubstitudeclick = function () {
var raw = SampleSubstitude();
var obj = ko.utils.unwrapObservable(new raw());
this.delegations.push(obj);
}
};
Unfortunately all values pushed to my array are empty. Can anybody give me a hint, what's wrong about here?
Share Improve this question edited Aug 4, 2017 at 9:47 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Jan 7, 2013 at 11:19 redflag237redflag237 2244 silver badges16 bronze badges 2- It's Substitute, not Substitude. That's more of a grammar thing, but I had to say it. – Jesse Hattabaugh Commented Nov 17, 2013 at 0:12
- @arkanciscan: It's more of a spelling thing. Not related to grammar. ;-) – T.J. Crowder Commented Feb 20, 2014 at 15:14
3 Answers
Reset to default 8If all of the values are observable, then you don't need a proxy method between your Person
constructor and your object:
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]"
}
You should set values to the observable op the person
person().id = id;
person().name = name;
person().loginName = login;
person().email = email;
Problem solved:
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);
}
variables were set private before, didn't took notice about it. thanks for help all.