JavaScript Durandal child viewmodel

假装没事ソ 提交于 2019-12-25 02:57:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!