Durandal:ViewModel在常见视图中不起作用

时间:2013-08-13 10:03:25

标签: durandal

我正在尝试在我的shell页面中为我的导航/菜单(垂直)创建一个公共区域。然后,在每次页面转换时,将通过新的菜单集更新此shell的菜单区域。在我的shell.js内部继承人。

define(function (require) {
var router = require('durandal/plugins/router');
var Menu = function (name, children) {
    this.name = ko.observable(name);
    this.children = ko.observableArray(children);
}

var Menus = ko.observableArray([]);

function GetDSRList() {
    router.navigateTo('#/reports/list2');
}

function activate() {


    router.mapNav('reports/index', 'viewmodels/reports/index', 'Reports');
    router.mapNav('reports/list', 'viewmodels/reports/list', 'List');
    router.mapNav('reports/list2', 'viewmodels/reports/list2', 'List2');
    router.mapNav('home/menu', 'viewmodels/home/menu', 'Main');
    return router.activate('home/menu');
}

function viewAttached() {
    $(document).ready(function () {
        $("#panelbar").kendoPanelBar({
            expandMode: "single"
        }),

        $("#splitter").kendoSplitter({
            panes: [
                    { collapsible: false, resizable: false, size: "20%" },
                    { collapsible: false }
                   ]
        });
    });
}

function token() {
    return $("input[name='__RequestVerificationToken']").val();
}

var shell = {
    router: router,
    activate: activate,
    viewAttached: viewAttached,
    GetDSRList: GetDSRList,
    Menus: Menus,
    token: token,
    Menu: Menu
}

return shell;  

对于View(shell.html)

<div style="height:100%;">
    <div class="loader" data-bind="css: { active: router.isNavigating }">

    </div>    

    <div id="splitter" style="border:0;height:100%">

            <div class="nav-panel-section">
                <ul id="panelbar">

                   <li data-bind="foreach : Menus">
                        <span class="k-link k-header" data-bind="text: name">
                             <span class="k-icon k-i-arrow-s k-panelbar-expand"></span>
                        </span>
                            <ul data-bind="foreach: children">
                                <li>
                                    <span class="k-link" data-bind="text: $data,click:this.GetDSRList"></span>
                                </li>
                            </ul>
                    </li>


                </ul>
            </div>


            <a href="#/reports/list2">Log off</a>
            <div id ="partialContent">

                    <div class="container-fluid page-host">
                        <!--ko compose: { 
                            model: router.activeItem, //wiring the router
                            afterCompose: router.afterCompose, //wiring the router        
                            cacheViews:true //telling composition to keep views in the dom, and reuse them (only a good idea with singleton view models)
                        }--><!--/ko-->

                         <form method="post" action="Account/LogOff" id="logoutForm" >
                            <input type="hidden" name="__RequestVerificationToken" data-bind="value:token"/>
                        </form>
                    </div>

            </div>
    </div>        
</div>

然后转到网址'#/ reports / list2'我有这个viewmodel(list2.js):

define(function (require) {

var shell = require('viewmodels/home/shell')


function activate() {
    shell.Menus = [];
    shell.Menus = [
                new shell.Menu("Menu3", ["A", "B", "C"]),
                new shell.Menu("Menu5", ["A", "B", "C"])
            ];
}

var list2 = {
    activate : activate
}

return list2;

});

但是除非你刷新整个页面,否则shell的菜单UI不会改变。我在这里错过了什么吗?日Thnx

1 个答案:

答案 0 :(得分:0)

您的问题是您创建了一个observableArray,然后在新的视图模型加载上擦除它。您需要使用observableArray的setter函数来维护它与DOM的连接(请不要更正我的语法,尝试KISS !!)

function activate() {
    shell.Menus = ([]);
    shell.Menus = ([
                new shell.Menu("Menu3", ["A", "B", "C"]),
                new shell.Menu("Menu5", ["A", "B", "C"])
            ]);
}

只需添加parans即可解决问题。当你清理时使用parans,当你设置时使用parans。