问题
I am using DurandalJS. My view welcome.html
has a child view mymodule
:
welcome.html:
<div>
<!--ko compose: {model:'viewmodels/mymodule', activate: true}-->
<!--/ko-->
</div>
The activate
function is called in mymodule
every time the view welcome.html
is activated, as expected. But when the welcome.html
is deactivated (I navigate to another view) the functions canDeactivate
and devativate
are not called in mymodule.js.
How can this be fixed?
回答1:
Using activate:true on the compose binding does not manage the full lifecycle of your view model, only the activate function will be called with that approach. (As you are seeing).
To get the all lifecycle events you need to use an activator to manage the lifecycle of the view model.
You have several options for doing this, see the durandal documentation on the matter for more insight. http://durandaljs.com/documentation/Hooking-Lifecycle-Callbacks/ Specifically, look at the "Activator Callbacks" section of that page.
It basically boils down to using the router to activate your view models or creating an activator yourself.
来源:https://stackoverflow.com/questions/16060570/javascript-durandal-child-viewmodel