PagerJS / KnockoutJS子页面嵌套绑定

时间:2016-06-29 18:16:05

标签: javascript knockout.js data-binding pagerjs

我在使用带有pager.js和knockout.js的嵌套对象时遇到了问题。

我有一个单页视图模型,它加载函数中子页面的信息,并在“withOnShow”数据绑定中调用该函数。

子页面本身加载正常,但是我无法使用“with”data-bind访问我传递给子页面的对象的任何元素。

我尝试过以下方式加载元素:

  • $ data.etc
  • $ data.notifications.etc
  • notifications.notifications.etc
  • notifications.etc
  • 通知()。等
  • 通知()。notifications.etc
  • self.notifications.notifications.etc
  • self.notifications()。notifications.etc

我也尝试将我的observable元素的类型从“ko.observable([])”更改为“ko.observableArray([])”,以及在使用和不使用“with”绑定之间切换。相关代码如下(这是在我的主页jsp中的脚本标记中的viewmodel函数中):

self.notifications = ko.observable({});
self.loadNotifications = function(callback, page) {
    $.getJSON("${pageContext.request.contextPath}/static/json/notificationtest.json", function(notifications) {
        ko.mapping.fromJS(notifications, {}, self.notifications);
        callback(self);
        console.log(self.notifications());
    });
}

相关的HTML如下(这是在我的主页jsp中):

<div data-bind="page: {id: 'profile', role: 'start'}">
   <div data-bind="page: {id: 'notifications', sourceOnShow: '${pageContext.request.contextPath}/action/secured/user/profile/notifications',
    title: 'Notifications', with: self.notifications, withOnShow: loadNotifications}">
</div>

我正在尝试做的一个例子(这段代码在我的子页面jsp中):

<div data-bind="if: $data.manager">
<!-- then one kind of view with a lot of stuff going on happens -->
<div data-bind="ifnot: $data.manager">
<!-- otherwise this view pops up -->

这只是我尝试访问数据的一种方式。我还有一些我访问的布尔值,以及一些用于填充表单的数组。 JSON如下:

{   
"notifications": {
    "sameList": false,
    "displayMessage1": true,
    "displayMessage2": false,
    "displayMessage3": true,
    "dm1Global": false,
    "dm2Global": true,
    "dm3Global": true,
    "manager": true,
    "contacts": [
        {"name": "NAME1"}, {"name": "NAME2"}, {"name": "NAME3"}
    ],
    "contacts1": [
        {"name": "NAME4"}
    ],
    "contacts2": [
     ""   
    ],
    "contacts3": [
     {"name": "NAME5"}, {"name": "NAME6"}   
    ]
    }
}

有什么见解?谢谢! (如果我问问题不好,请告诉我,如果我需要提供更多信息,请告诉我!)

0 个答案:

没有答案