0

我正在做一个带有菜单和子菜单的导航列表。

我有以下结构:

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());

所以,检查一下。当我尝试提醒 item 变量时,结果会正确显示。当我尝试提醒新的 Menu 对象时,结果只是显示{}. 为什么会这样?

谢谢大家的帮助!

4

1 回答 1

0

self.Navigations = ..不会更新现有的 observable 数组因此,视图绑定的可观察数组永远不会更新,视图也永远不会更新。

改为使用self.Navigations(..)

于 2013-10-20T15:55:49.993 回答