ViewAttached事件未激发

ViewAttached event is not fired

本文关键字:事件 ViewAttached      更新时间:2023-09-26

我使用的是durandal master details示例,它使用的是composition。我有一个视图模型,其中定义了一些事件,

 var ctor = function(name, description) {
        this.name = name;
        this.description = description;
    };
   
    ctor.prototype.activate = function() {
        system.log('Model Activating', this);
    };
   
    ctor.prototype.deactivate = function () {
        system.log('Model Deactivating', this);
    };
    ctor.prototype.viewAttached = function (view) {
      system.log('this is not called !', this);
    };

除了viewAttached之外的所有事件都被激发。我在这里找不到原因。。

实际上,我在GithHub存储库中使用的是Durandal的最新版本。在查看合成模块后,发现他们已将其重命名为compositionComplete。这是意料之中的事。。

 ctor.prototype.compositionComplete= function (view) {
      system.log('works!', this);
    }; 

编辑
Durandal 2.0发布后,在文档中,我们有attached事件,而不是viewAttached

如果您使用的是Durandal 2.0,"viewAttached"方法已重命名为"attached"。

如果没有更多信息,我唯一可以建议检查的是Durandal文档中的这条注释:

注意:如果您设置了cacheViews:true,那么viewAttached将只在视图第一次显示时调用,即在初始绑定时调用,因为从技术上讲,视图只附加一次。如果要覆盖此行为,请在组合绑定上设置alwaysAttachView:true。

尝试在绑定中设置alwaysAttachView:true,看看这是否有效。