0

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!

Kiwanax
  • 1,265
  • 1
  • 22
  • 41
  • possible duplicate of [Setting value of Observable not updating in Knockout](http://stackoverflow.com/questions/19391415/setting-value-of-observable-not-updating-in-knockout) – PW Kad Oct 20 '13 at 15:56

1 Answers1

0

self.Navigations = .. does not update the existing observable array. As such, the view-bound observable array is never update and the view is never updated.

Use self.Navigations(..) instead.

user2864740
  • 60,010
  • 15
  • 145
  • 220