2

我在 main.js 中定义了根路由器,并在其中拥有导航代码。在页面上有一个列表,我想从选定的列表项中打开查看页面。我可以通过在 html 页面中使用 $root.router.retreive() 来获取页面上的数据。

如何在其相应的 viewModel 中获得该值?

导航代码:

self.gotoPage = function(data, event) 
{
   self.router.store(data.id);
   self.router.go(event.target.id);
};

绑定代码:

oj.Router.sync().then(
  function () {
    // bind your ViewModel for the content of the whole page body.
    ko.applyBindings(new RootViewModel(), document.getElementById('globalBody'));
  },
  function (error) {
    oj.Logger.error('Error in root start: ' + error.message);
  }
);
4

1 回答 1

1

您始终可以使用$module来访问活动视图模型中定义的任何变量。示例:$module.router.retreive()用于 HTML 绑定。

如果您在 ViewModel 中查找值,您可以扩展router.moduleConfig上下文并将其传递为moduleParams.

或者,您可以使用ko.dataForHTML 选择器从绑定上下文中获取数据。

根据评论...

setup: function(routerParent) {
    var router, latestState;
    if (routerParent == undefined) {
        router = oj.Router.rootInstance;
    } else {
        if (routerParent.getChildRouter(routerParent.currentState().id)) {
            routerParent.getChildRouter(routerParent.currentState().id).dispose();
        }
        router = routerParent.createChildRouter(routerParent.currentState().id);
    }
    router.configure(function(stateId) {
        if (stateId) {
            latestState = stateId;
            return new oj.RouterState(stateId);
        }
    });
    oj.Router.sync().then(function(value) {
        //console.info("Calling Router Sync: Result = " + value);
    }, function(reason) {
        console.info("Calling Router Sync: Result = " + reason);
    });
    return $.extend(router, { latestState: latestState });
},
于 2016-12-20T15:56:26.157 回答