如何使用小胡子呈现对象数组的数组

时间:2020-12-08 14:31:24

I am trying to render an array of arrays of objects with a mustache template in javascript, and I have not found anyone else who has asked this question. I can render an array of objects just fine, but I can't figure out how to render an array of them. I could assign each nested array to its own variable I suppose, but there could be any number of them, so I really need to keep them as an array.

我试图用javascript中的胡子模板渲染一个对象数组的数组,我没有找到其他人问过这个问题。我可以很好地渲染一个对象数组,但我无法弄清楚如何渲染它们的数组。我可以将每个嵌套数组分配给我自己的变量,但是可能有任意数量,所以我真的需要将它们保存为数组。

Here is the type of data I need to render:

这是我需要呈现的数据类型:

[
    [
        { id: 12345, name: "Billy" },
        { id: 23456, name: "Joe" },
        { id: 34567, name: "Jenny" }
    ],
    [
        { id: 45678, name: "Amy" },
        { id: 56789, name: "Julie" },
        { id: 67890, name: "Sam" }
    ]
]

The outer array could contain any number of nested arrays, and each nested array could contain any number objects.

外部数组可以包含任意数量的嵌套数组,每个嵌套数组可以包含任何数字对象。

I don't know if it's possible with mustache. I tried using a function, and this is the first time I've ever used a function with mustache, so maybe I'm doing something wrong. I am calling it from the render function of a Backbone View. The array of arrays (shown above) is part of the view's model's attributes. So here is what I tried.

我不知道胡子是否可能。我尝试使用一个函数,这是我第一次使用带胡子的函数,所以也许我做错了。我从Backbone View的渲染功能中调用它。数组数组(如上所示)是视图模型属性的一部分。所以这就是我的尝试。

render:
    function ()
    {
        this.model.attributes.getList =
            function ()
            {
                return  function (str, func) { return 'What in the world should I return here?'; }
            }

        this.$el.html (Mustache.render ($ ('#detail-template').html (), this.model.attributes));

        return this;
    },

And here is the section of my template where I am attempting to use the function.

这是我模板的一部分,我试图使用该功能。

{{#getList}}
    {{name}}
{{/getList}}

I am pretty sure {{name}} doesn't belong in there, but I have no idea what else I would put in there.

我很确定{{name}}不属于那里,但我不知道我还会把它放在那里。

I tried returning func (str), but all it printed was a long string that contained [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

我试过返回func(str),但它打印的都是一个包含[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object]的长字符串对象],[对象]

I could not use that string as a json object, it was just a string.

我无法将该字符串用作json对象,它只是一个字符串。

I'm somewhat new to both backbone and mustache, so I thought someone might have a "best practice" solution to this, or at least could tell me if it is impossible so I don't waste any more time on it. I could not find a similar question anywhere on the Internet.

我对骨干和小胡子都有点新意,所以我觉得有人可能会有一个“最佳实践”的解决方案,或者至少可以告诉我这是不可能的,所以我不再浪费时间了。我无法在互联网上的任何地方找到类似的问题。

1 个解决方案

#1


1  

This question is 2 years old but I guess better late than never. You can use {{.}} to reference the current element in an array.

这个问题是2岁,但我觉得迟到总比没有好。您可以使用{{。}}来引用数组中的当前元素。

context = [
    [
        { id: 12345, name: "Billy" },
        { id: 23456, name: "Joe" },
        { id: 34567, name: "Jenny" }
    ],
    [
        { id: 45678, name: "Amy" },
        { id: 56789, name: "Julie" },
        { id: 67890, name: "Sam" }
    ]
]

template = "
    {{#context}}
        {{#.}}
            <span id={{id}}>{{name}}</span>
        {{\.}}
    {{/context}}
"

#1


1  

This question is 2 years old but I guess better late than never. You can use {{.}} to reference the current element in an array.

这个问题是2岁,但我觉得迟到总比没有好。您可以使用{{。}}来引用数组中的当前元素。

context = [
    [
        { id: 12345, name: "Billy" },
        { id: 23456, name: "Joe" },
        { id: 34567, name: "Jenny" }
    ],
    [
        { id: 45678, name: "Amy" },
        { id: 56789, name: "Julie" },
        { id: 67890, name: "Sam" }
    ]
]

template = "
    {{#context}}
        {{#.}}
            <span id={{id}}>{{name}}</span>
        {{\.}}
    {{/context}}
"