KnockoutJS - creating object doesn't work
- by Kiwanax
I'm doing a navigation list with menus and submenus.
I have the following structure:
function Menu(navigation) {
this.NavigationUrl = ko.observable(navigation.NavigationUrl);
this.NavigationTitle = ko.observable(navigation.NavigationTitle);
this.NavigationDescription = ko.observable(navigation.NavigationDescription);
var mappedChildren = ko.utils.arrayMap(navigation.Children, function (child) {
return new Menu(child);
});
this.Children = ko.observableArray(mappedChildren);
}
function DashboardViewModel() {
var self = this;
self.LoggedUser = ko.observable("");
self.Navigations = ko.observableArray([]);
$.get('/Home/DashboardDependencies', {}, function (result) {
self.LoggedUser(result.LoggedUser);
var mappedNavigations = ko.utils.arrayMap(result.Navigations, function (item) {
var menu = new Menu(item);
// When I alert item, the result appears properly:
// { "NavigationTitle": "blah", "NavigationDescription": "bleh" [...] }
alert(JSON.stringify(item));
// But when I alert the new menu object, the result doesn't appear:
// Just: "{}"
alert(JSON.stringify(menu));
return menu;
});
self.Navigations = mappedNavigations;
});
}
ko.applyBindings(new DashboardViewModel());
So, check it out. When I try to alert the item variable, the result appears properly. When I try to alert the new Menu object, the result just show {}. Why this' happening?
Thank you all for the help!