0
我正在做一個包含菜單和子菜單的導航列表。KnockoutJS - 創建對象不起作用
我有以下結構:
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());
因此,檢查出來。當我嘗試提醒項目變量時,結果顯示正確。當我嘗試提醒新的Menu對象時,結果只顯示{}
。爲什麼這個'發生?
謝謝大家的幫助!
[Observable的設置值在Knockout中未更新](http://stackoverflow.com/questions/19391415/setting-value-of-observable-not-updating-in-knockout) –