Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 757
redflag237 Avatar asked Aug 02 '26 13:08

redflag237


1 Answers

If 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]"
} 
like image 69
Konstantin Dinev Avatar answered Aug 07 '26 15:08

Konstantin Dinev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!