如何检测何时在Durandal中加载合成的html文件(在激活器激活新项目之后)

时间:2013-11-12 00:16:08

标签: durandal durandal-2.0

我正在使用激活器,我可以检测何时加载甚至激活了新的视图模型但是如何检测何时加载并显示其html对应物?我只需要在旧的html(组合/嵌入页面)被新的html替换后不久做一些事情。请注意,我对嵌入式html的更改感兴趣,而不是初始构图。

注意2:我需要从父视图模型中检测到这一点 - 具有激活器(加载的那个,而不是 加载的那个)。

2 个答案:

答案 0 :(得分:0)

您可以使用binding event in your view model

  

当组合引擎获取您的视图和对象,并使用绑定器将它们绑定在一起时,在绑定发生之前和之后执行另外两个回调。绑定之前调用绑定(视图),然后在绑定之后调用bindingComplete(view)。每次调用都会将视图传递给您的对象。

答案 1 :(得分:0)

另一个答案是不正确的 -

您需要使用附加的回调并将其公开给需求模块。你可以这样做 -

function attached() {
    console.log('View has been attached');
}

如果你想在这里发起一个事件,让像父视图模型这样的订阅者可以这样做 -

var callback;

function activate(callbackFunction) {
    callback = callbackFunction;
}
function attached() {
    callbackFunction();
}

这个新视图是通过传入的绑定组成的。

define(['viewmodels/otherViewModel'], function (otherViewModel) {

    function thisIsACallback () {
        alert('Called back');
    }

    // Instantiate the other view model
    function callOtherViewModel () {
        var whatever = new otherViewModel(thisIsACallback);
    }

});

并在视图中显示。

修改

如果这不是你想要的,那么你可以随时观看activeInstruction或查看路由器是否还在导航 -

var token = router.activeInstruction.subscribe(function() {
    // Do whatever you want here
}

var token = router.isNavigating.subscribe(function() {
    // Do whatever you want here
}

只需查看其他公开方法的API文档

http://durandaljs.com/documentation/api/#module/router/class/Router