[英]KnockoutJS - creating object doesn't work
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. 当我尝试提醒item变量时,结果将正确显示。 When I try to alert the new Menu object, the result just show {}
. 当我尝试警告新的Menu对象时,结果仅显示{}
。 Why this' happening? 为什么会这样呢?
Thank you all for the help! 谢谢大家的帮助!
self.Navigations = ..
does not update the existing observable array. self.Navigations = ..
不更新现有观察的阵列。 As such, the view-bound observable array is never update and the view is never updated. 这样,视图绑定的可观察数组永远不会更新,视图也永远不会更新。
Use self.Navigations(..)
instead. 请改用self.Navigations(..)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.