javascript - KnockoutJS and Recursive Template - Stack Overflow

I have seen this question, and using its method throws up an error on the JS console Uncaught SyntaxErr

I have seen this question, and using its method throws up an error on the JS console Uncaught SyntaxError: Unexpected token ).

I am trying to take a recursive array of categories, that have a Children property which is an array of categories, and build them out using a jquery template. Every method I have tried results in some syntax error. I have verified that the object is showing up properly in javascript (its ing from MVC3, using @Html.Raw(Json.Encode(Model.Categories)) to get it into a JS array). Here is the original csharp class

public class CategoryTreeModel
{
    public int Id { get; set; }
    public string Name{ get; set; }
    public bool Selected { get; set; }
    public bool HasChildren { get { return Children.Count > 0; } }
    public List<CategoryTreeModel> Children { get; set; }
}

This original html that calls the first level of the template:

<ul class="nav"  data-bind="template: {name: 'categories_template', foreach: categories}">
        </ul>

and the template itself:

<script type="text/html" id="categories_template">
<li id="category_${Id}" class="category_header">${Name}
   {{if $data.HasChildren }}
        <ul data-bind='template: { name: "categories_template", foreach: Children}'>
        </ul>
   {/if}
</li>      

The template works if I take out the children section of it, rendering the first level properly. I get Uncaught SyntaxError: Unexpected token ) when I use the code as is. What am I doing wrong?

I have seen this question, and using its method throws up an error on the JS console Uncaught SyntaxError: Unexpected token ).

I am trying to take a recursive array of categories, that have a Children property which is an array of categories, and build them out using a jquery template. Every method I have tried results in some syntax error. I have verified that the object is showing up properly in javascript (its ing from MVC3, using @Html.Raw(Json.Encode(Model.Categories)) to get it into a JS array). Here is the original csharp class

public class CategoryTreeModel
{
    public int Id { get; set; }
    public string Name{ get; set; }
    public bool Selected { get; set; }
    public bool HasChildren { get { return Children.Count > 0; } }
    public List<CategoryTreeModel> Children { get; set; }
}

This original html that calls the first level of the template:

<ul class="nav"  data-bind="template: {name: 'categories_template', foreach: categories}">
        </ul>

and the template itself:

<script type="text/html" id="categories_template">
<li id="category_${Id}" class="category_header">${Name}
   {{if $data.HasChildren }}
        <ul data-bind='template: { name: "categories_template", foreach: Children}'>
        </ul>
   {/if}
</li>      

The template works if I take out the children section of it, rendering the first level properly. I get Uncaught SyntaxError: Unexpected token ) when I use the code as is. What am I doing wrong?

Share Improve this question edited May 23, 2017 at 11:55 CommunityBot 11 silver badge asked Nov 22, 2011 at 18:32 KyeoticKyeotic 19.9k12 gold badges72 silver badges132 bronze badges 1
  • 5 Note to future visitors, this was knockout1.3, this template model has been deprecated in knockout 2.0 – Kyeotic Commented May 29, 2012 at 16:11
Add a ment  | 

2 Answers 2

Reset to default 4

Your last {/if} should be {{/if}}

Here is a sample: http://jsfiddle/rniemeyer/vbKVC/

I think, I have a little better solution. Please take a look:

http://jsfiddle/nonsense66/Bzekr/

Template:

<script id="treeElement" type="text/html">
    <li>
        <span data-bind="text: name"></span>
        <ul data-bind="template: { name: 'treeElement', foreach: children }">
        </ul>
     </li>
</script>    

<ul data-bind="template: { name: 'treeElement', foreach: $data.treeRoot }"></ul>

Javascript:

var viewModel = {
    treeRoot: ko.observableArray()
};

var TreeElement = function(name, children) {
   var self = this;
   self.children = ko.observableArray(children);
   self.name = ko.observable(name);
}

var tree = [
    new TreeElement("Russia", [
        new TreeElement("Moscow")
    ]),
    new TreeElement("United States", 
        [
            new TreeElement("New York", [ 
                new TreeElement("Harlem"),
                new TreeElement("Central Park")
            ]) 
        ])
];

viewModel.treeRoot(tree);

ko.applyBindings(viewModel);

Hope this helps

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744748125a4591428.html

相关推荐

  • javascript - KnockoutJS and Recursive Template - Stack Overflow

    I have seen this question, and using its method throws up an error on the JS console Uncaught SyntaxErr

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信