0

In Master Detail sample on Durandal website, project.activate function is invoked two times each time when I change project in dropdown list.

First activation is invoked in write method of Activator. Second activation is invoked in composition.bindAndShow.

If I'm right, an obvious fix would be replacing (in index.html) this

<!--ko compose: activeProject--><!--/ko-->

with this

<!--ko compose: { model: activeProject, activate: false }--><!--/ko-->

but Durandal documentation says in Using Composition > Additional Settings > Activate:

Note: If you are using an activator, it will control the activation call and you should not attempt to manipulate it via the compose binding.

So I shouldn't do that. What is a proper way to fix this?

Pol
  • 5,064
  • 4
  • 32
  • 51

1 Answers1

0

As a workaround what about setting a runInit flag in the ctor, which gets set to false during activate?

var ctor = function(name, description) {
    this.runInit = true;
    this.name = name;
    this.description = description;
};


ctor.prototype.activate = function() {
    if (this.runInit){
        this.runInit = false;
        system.log('Model Activating', this);
    }
};

Live version available at http://dfiddle.github.io/dFiddle-2.0/#master-detail/so19719038

In addition I'd raise that as an issue https://github.com/BlueSpire/Durandal/issues?state=open to check if the double activation is expected or not.

RainerAtSpirit
  • 3,723
  • 1
  • 17
  • 18
  • I'm doing something simillar at the moment but I'm blocking first activation and allowing second activation (from composition) as it works better with Knockout. The downside is that I've to do that in every viewmodel affected. A plugin solution would be better... If you know how to do that (block activation from Activator) in plugin I would love to hear! – Pol Nov 01 '13 at 18:08
  • Having said that I still need Activator for deactivation now and maybe for more in future. Just don't need it's activate invoke :) – Pol Nov 01 '13 at 18:38
  • As said above I'm not sure if the behavior you're seeing is by design. It would be best to raise it as an issue. – RainerAtSpirit Nov 01 '13 at 22:01