Durandal Compose: activation method is not always named

In a SPA, I have a view in which I include another view using the binding layout. This subview (child view) has an activation method that 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 return to it (without refreshing the browser), the activation method on the child view is no longer called. How do I get this to be called every time the parent view is loaded?

+3


source to share


1 answer


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

If you want to use the first approach, you must create a child router on the viewModel and create a binding router

instead of a binding compose

. It might be overkill in this case, so I'll skip it.

The best approach, given the information you provide, is to not try to automatically connect to the activation lifecycle. So instead of calling the function, activate()

activate it, call it init()

:

function folIndexViewModel() {

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

      



In your parent, activate the activation function init

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

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

      

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

+2


source







All Articles