1

I have a problem creating observable array of observables. I search on google but didn't find a solution. It may be something simple that I can't notice, because I'm new to Knockout. But I have the model:

eventsModel = function () {
   var self = this;

   self.endTimeInMinutes = ko.observable();
   self.events = ko.observableArray([]);
   self.startTripTime = ko.observable();
   self.endTripTime = ko.observable();
}

and I want to have an observables items in my array so I write a ViewModel and bind the model.

eventItemViewModel = function(o) {
    var self = this;

    self.BeginInMinutes = ko.observable(o.BeginInMinutes);
    self.Type = ko.observable(o.Type);
};

var events = new eventsModel();
ko.applyBindings(events);

And I'm fetching the data using AJAX:

function GetEvents() {
    $.ajax({
        url: "Contact.aspx/GetEvents",
        async: true,
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            var temp = data.d;
            endTimeInMinutes = temp["EndInMinutes"];
            eventsArr = temp["Events"];
            eventsArray = JSON.parse(JSON.stringify(eventsArr));
            events.events(eventsArray);
        },
    });
}

After this I have an observable array but without observables values inside. Now I trying to add in my AJAX method:

events.events(ko.utils.arrayMap(eventsArray, function(eve) {return new eventItemViewModel(eve); }));

But if I do console.log on this, then I am getting array of objects, and in each object there is a BeginInMinutes and Type, but it's value is like function d()... etc.

I'm really get stucked with it, and I believe I made some very simple mistake somewhere.

Thanks for helping me.

8
  • what do you want exactly, Do you want the items in the observableArray to be observable or just js object ? I think the question is not very clear Commented Apr 28, 2017 at 8:05
  • by the way, the result which you get function d() is an evidence that you have an observable objects in the observableArray. Is it what you already want ?? Commented Apr 28, 2017 at 8:06
  • put the code that you are using for the Console.log, you have to call that function to get the data of the BeginInMinutes Commented Apr 28, 2017 at 8:08
  • Ok, so if it's an evidence, then maybe the problem is how to show the exact data instead of showing function d().... In the console.log the data is located under [[Scopes]] -> Closure. So maybe my problem is about to show this proper data, if you say that it's already an observableArray of observables. Commented Apr 28, 2017 at 8:10
  • you can show your data by the following command BeginInMinutes(), just add the parentheses to the name of the property. Commented Apr 28, 2017 at 8:11

1 Answer 1

1

You already got an observableArray with observable element inside it,
Your problem really is with getting those values
use this code to get the real value of the first element in the array.

console.log(events.events()[0].BeginInMinutes());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.