激活方法并不总是叫

激活方法并不总是叫

本文介绍了迪朗达尔撰写:激活方法并不总是叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个水疗中心,我有一个观点我在其中包括使用撰写结合另一种观点。此子视图(子视图)的父视图被加载时,这就是所谓的激活方法。

In a SPA,I have a view in which I include another view using the compose binding. This sub view (child view) has an activate method which is called when the parent view is loaded.

<div data-bind="compose: 'viewmodel/fol/index'"></div>

然而,当我浏览从父视图离开,然后回去吧(无需刷新浏览器),在子视图的激活方法不再调用。我怎么可以强制使用每个父视图被加载时被调用?

However, when I navigate away from the parent view and then go back to it (without refreshing the browser), the activate method in the child view is not longer called. How can I force this to be called each time the parent view is loaded?

推荐答案

既然你没有利用路由器在这里,孩子的行为视图模型不尊重充分激活生命周期。有您可以采取两种方法:(1)利用路由器或(b)手动激活孩子。

Since you are not leveraging the router here, the behavior of the child viewModel will not respect the full activation lifecycle. There are two approaches you can take: (a) leverage the router or (b) manually activate the child.

如果你想采取第一种方式,你可以创建的视图模型孩子路由器,并创建一个路由器,而不是绑定撰写绑定。这可能是矫枉过正,在这种情况下,所以我会跳过它。

If you wanted to take the first approach, you would create a child router on the viewModel and create a router binding instead of a compose binding. That might be overkill in this case so I will skip it.

更好的方法,给你在这里提供的信息,将不尝试自动挂接到激活生命周期。因此,而不是调用你激活()功能激活,称之为的init()

The better approach, given the information you've provided here, would be to not try to hook into the activation lifecycle automatically. So instead of calling your activate() function activate, call it init():

function folIndexViewModel() {

  this.init = function() {
    ...
  };
}

在您的父母,您的激活函数调用初始化

In your parent, have your activate function call init

var child = require('viewmodel/fol/index');
function parentViewModel() {

    this.activate = function() {
        child.init();
    }
}

请注意,如果你的孩子是一个单身这种方法才有效。在这篇文章的底部上的更多信息:。

Note that this approach only works if your child is a singleton. More information on that at the bottom of this article: http://durandaljs.com/documentation/Creating-A-Module.html.

这篇关于迪朗达尔撰写:激活方法并不总是叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:34