在Durandal的组合视图中激活功能

时间:2013-03-05 08:43:17

标签: durandal

我有像这样的Shell html

<div data-bind="compose: { model: 'ui/user/viewmodels/header', view: 'infoveave/user/views/header'}"></div>
<div class="container-fluid page-host">
    <!--ko compose: {
        model: router.activeItem,
        afterCompose: router.afterCompose,
        transition:'entrance'
    }--><!--/ko-->
</div>

和shell js一样

define(function(require) {
var router = require('durandal/plugins/router'),
    system = require('durandal/system'),
return {
    router: router,
    activate: function () {
        var self = this;
        router.mapAuto('ui/user/viewmodels');
        system.log('Sheel Activate Called');
        return router.activate('dashboard');
    }
};
});

问题是标题上的激活函数没有被调用但是仪表板上的激活函数被调用,我必须在标题中获取一些ajax内容并绑定它,我怎样才能实现这个

我想保持这个逻辑分离,因为我不希望我的shell有这个逻辑

以供参考我的标题(为简化起见,我已将我的复杂模型转换为简单的可观察

define(function (require) {
var router = require('durandal/plugins/router'),
    system = require('durandal/system');

this.userInfo = ko.observable('');

return {
    router: router,
    activate: function () {
         system.log('Got Called Now');
         //do some ajax stuff here and update userinfo
    }
};
});

标头html的最简单形式是

<span class="dropdown-notif" data-bind="text: userInfo"></span>

3 个答案:

答案 0 :(得分:7)

我认为你没有激活标题视图模型。

尝试将'activate:true'添加到标题视图中,如下所示组合绑定:

<div data-bind="compose: { 
    model: 'ui/user/viewmodels/header', 
    view: 'infoveave/user/views/header', 
    activate: true}">
</div>

答案 1 :(得分:1)

这可能是你的问题吗?: “除非存在激活器或激活,否则不执行激活器回调:在组合绑定上设置为true。”

来源:http://durandaljs.com/documentation/Hooking-Lifecycle-Callbacks/

答案 2 :(得分:0)

您是否在浏览器的控制台窗口中看到任何错误? (在Chrome中点击F12,然后点击控制台标签)。

我怀疑你会遇到一些阻止视图模型激活的错误。否则,您可以尝试这些更改:

壳:

define(function(require) {
    var router = require('durandal/plugins/router'),
        system = require('durandal/system'); // note the semi colon here

    return {
        router: router,
        activate: function () {
            var self = this;
            router.mapAuto('ui/user/viewmodels');
            // the line below to map the route to your view model may be required
            router.mapRoute('dashboard');
            system.log('Sheel Activate Called');
            return router.activate('dashboard');
        };
    };
});

标题视图模型:

define(function (require) {
    var router = require('durandal/plugins/router'),
        system = require('durandal/system');

    var userInfo = ko.observable('');

    return {
        router: router,
        userInfo: userInfo, // putting the property here should make it visible to the binding in the view
        activate: function () {
             system.log('Got Called Now');
             //do some ajax stuff here and update userinfo
        }
    };
});