最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Knockout unwrap complex objects - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 8

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]"
} 

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.

发布评论

评论列表(0)

  1. 暂无评论