使用KnockoutJs映射插件进行递归模板化

时间:2011-01-20 17:35:24

标签: javascript json mapping knockout.js

我正在尝试使用ko.mapping plugin在树上进行递归模板化,但我无法渲染它,除非我定义单独 每个级别的模板。

在以下情况中,我想重用 mvvmTreeViewGroupTemplate 对于 mvvmTreeViewSubGroups 也是如此,但这没有呈现,是 这个bug还是未实现的功能?

<div id="treeViewArea">
    <ul data-bind='template: { 
                            name: "mvvmTreeViewGroupTemplate", 
                            foreach: MvvmTreeItemGroups,
                            beforeRemove: function(elem) { $(elem).slideUp() },
                            afterAdd: function(elem) { $(elem).hide().slideDown() } 
                            }'>
    </ul>
</div>
        <script type="text/x-jquery-tmpl" id="mvvmTreeViewGroupTemplate">  <li> 
            <span data-bind="text: Title" class="mvvmTreeItemStyle"/></br/>     <ul data-bind='template: { 
                                    name: "mvvmTreeViewItemTemplate", 
                                    foreach: MvvmTreeItems, 
                                    beforeRemove: function(elem) { $  (elem).slideUp() }, 
                                    afterAdd: function(elem) { $  (elem).hide().slideDown() } 
                                    }'> 
            <ul data-bind='template: { 
                                    name: "mvvmTreeViewSubGroupTemplate", 
                                    foreach: this.MvvmTreeItemSubGroups, 
                                    beforeRemove: function(elem) { $  (elem).slideUp() }, 
                                    afterAdd: function(elem) { $  (elem).hide().slideDown() } 
                                    }'> 
            </ul> 
            </ul>  
    </li>  
    </script>
    <script type="text/x-jquery-tmpl" id="mvvmTreeViewSubGroupTemplate">  <li> 
            <span data-bind="text: Title" class="mvvmTreeItemStyle"/></br/>     
<ul data-bind='template: { 

                                    name: "mvvmTreeViewItemTemplate", 
                                    foreach: MvvmTreeItems, 
                                    beforeRemove: function(elem) { $  (elem).slideUp() }, 
                                    afterAdd: function(elem) { $  (elem).hide().slideDown() } 
                                    }'> 
            </ul>
      </li>  
    </script>

JSON和脚本看起来像这样,

var data = { 
            MvvmTreeItemGroups: [ 
            { 
                Id: 1, Title: 'Group 1', 
                MvvmTreeItemSubGroups: [{ 
                    Id: 1, Title: 'Group 11', 
                    MvvmTreeItems: [{ Id: 'i111', Title: 'Item 111' }, 
{ Id: 'i112', Title: 'Item 112'}] 
                }, 
                    { 
                        Id: 1, Title: 'Group 121', 
                        MvvmTreeItems: [{ Id: 'i121', Title: 'Item 
121' }, { Id: 'i122', Title: 'Item 122'}] 
                    }], 
                MvvmTreeItems: [{ Id: 'i11', Title: 'Item 11' }, { Id: 
'i12', Title: 'Item 12'}] 
            }, 
                { 
                    Id: 2, Title: 'Group 2', 
                    MvvmTreeItemSubGroups: [{ 
                        Id: 1, Title: 'Group 211', 
                        MvvmTreeItems: [{ Id: 'i211', Title: 'Item 
211' }, { Id: 'i212', Title: 'Item 212'}] 
                    }, 
                    { 
                        Id: 1, Title: 'Group 121', 
                        MvvmTreeItems: [{ Id: 'i121', Title: 'Item 
121' }, { Id: 'i122', Title: 'Item 122'}] 
                    }], 
                    MvvmTreeItems: [{ Id: 'i21', Title: 'Item 21' }, 
{ Id: 'i22', Title: 'Item 22'}] 
                }] 
        }; 

var viewModel = ko.mapping.fromJS(data);
        console.log(viewModel);
        ko.applyBindings(viewModel, treeViewArea);

1 个答案:

答案 0 :(得分:3)

我在this google thread得到了我的问题的答案。这不是一个“递归模板”问题,因为如果没有该名称的数组,模板就不知道如何渲染。

有两种方法可以解决此问题:

  1. 检查是否有MvvmTreeItemGroups数组 在渲染之前实际存在 像这样的模板,

    {{if $data.MvvmTreeItemGroups }}       
        <ul data-bind='template: { 
        name: "mvvmTreeViewGroupTemplate", 
        foreach: MvvmTreeItemGroups }'> 
        </ul>                 
    {{/if}}
    
  2. 使用in关键字进行检查 MvvmTreeItemGroups确实存在

    {if 'MvvmTreeItemGroups' in $data}}
        <ul data-bind='template: { 
        name: "mvvmTreeViewGroupTemplate", 
        foreach: MvvmTreeItemGroups
        }'> 
        </ul> 
    {{/if}}
    
  3. 完整的小提琴是http://jsfiddle.net/mikekidder/Xs7sy/