KnockoutJS - creating object doesn't work

Posted by Kiwanax on Stack Overflow See other posts from Stack Overflow or by Kiwanax
Published on 2013-10-20T15:51:07Z Indexed on 2013/10/20 15:53 UTC
Read the original article Hit count: 236

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!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about asp.net-mvc-4