1

我有这样的 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 内容并绑定它,我该如何实现

我想保持这个逻辑分开,因为我不希望我的外壳有这个逻辑

供参考我的标题(为了简化我已经将我的复杂模型转换为一个简单的可观察的

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
    }
};
});

header html 的最简单形式是

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

3 回答 3

7

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

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

<div data-bind="compose: { 
    model: 'ui/user/viewmodels/header', 
    view: 'infoveave/user/views/header', 
    activate: true}">
</div>
于 2013-03-07T13:39:24.740 回答
1

这可能是您的问题吗?:“除非存在激活器或在撰写绑定上设置了 activate:true,否则不会执行激活器回调。”

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

于 2013-07-10T15:14:44.353 回答
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
        }
    };
});
于 2013-03-05T15:26:49.577 回答