aleczandru
aleczandru

Reputation: 5449

Getting data from an object inside an object

Hi I am just learning knockout and I am facing a problem that I can not seem to understand. I have this object:

    var studentPersonalDetails = ko.observable();
    var isInitialized = false;

    var vm = {
        //bindable
        title: ko.observable('Profile'),
        dataLoading: ko.observable(false),
        hasErrors: ko.observable(false),
        errorMessage: ko.observable(''),

        //data
        profileStudentPersonalDetails: studentPersonalDetails,      

        //operations
        activate: activate

    };

    return vm;

profileStudentPersonalDetails is the equivalent of this C# object from the server:

    public int? StudentNumber { get; set; }
    public string Supervisor { get; set; }
    public bool CanEdit { get; set; }
    public string PersonId { get; set; }     
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string FullName { get; set; }

And is populated from a service.My problem is that I do know how to access the data from this object so I can display it on a html view.

I have tryed this versions:

<strong data-bind="text: StudentNumber">
<strong data-bind="text: profileStudentPersonalDetails.StudentNumber">

But non seem to work.The data gets populated into the object the right way of that I am sure and I am able to acces the other fields from the data for example the title:

<strong data-bind="text: title">

And this works.

How can I access the data?

Upvotes: 0

Views: 48

Answers (1)

Ben McCormick
Ben McCormick

Reputation: 25718

I think you want

profileStudentPersonalDetails().StudentNumber

observables are functions. To get the objects they represent, you need to call the function. You can then access the property off the result of the function.

Upvotes: 3

Related Questions