Search code examples
javascriptjqueryjquery-uiknockout.jsknockout-2.0

Knockout unwrap complex objects


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?


Solution

  • 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.