【问题标题】:Activate Function in composed view in Durandal在 Durandal 的组合视图中激活函数
【发布时间】:2014-06-02 20:39:35
【问题描述】:

我有这样的 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 有这个逻辑

作为参考我的标题(为了简化我已经将我的复杂模型转换为一个简单的 observable

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>

【问题讨论】:

    标签: durandal


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        您是否在浏览器的控制台窗口中看到任何错误? (在 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
                }
            };
        });
        

        【讨论】:

        • 控制台中没有记录错误,而该属性对于视图的绑定是可见的,该函数没有被执行。 (PS:shell.js 中缺少分号,因为我在将其发布到 stackoverflow 之前删除了几行)
        猜你喜欢
        • 1970-01-01
        • 2013-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-02
        • 2014-09-22
        • 2015-12-19
        • 2013-05-30
        相关资源
        最近更新 更多