JavaScript Durandal子视图模型 [英] JavaScript Durandal child viewmodel

查看:49
本文介绍了JavaScript Durandal子视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DurandalJS。我的视图 welcome.html 有一个子视图 mymodule

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>

activate 函数在<$中被调用如预期的那样,每次激活视图 welcome.html 时,c $ c> mymodule 。但是,当 welcome.html 被停用(我导航到另一个视图)时,功能 canDeactivate

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.

如何解决?

推荐答案

在compose绑定上使用activate:true不能管理视图模型的整个生命周期,只能使用该方法来调用activate函数。 (如您所见)。

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.

您可以通过多种方式进行操作,有关此问题的更多信息,请参阅有关此事项的durandal文档。
http://durandaljs.com/documentation/Hooking-Lifecycle-Callbacks/
具体来说,请查看该页面的激活器回调部分。

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.

这篇关于JavaScript Durandal子视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆