如何用胡子渲染对象数组 [英] How to render array of arrays of objects with mustache

查看:84
本文介绍了如何用胡子渲染对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用javascript中的小胡子模板渲染对象数组的数组,但没有找到其他人问过这个问题.我可以很好地渲染对象数组,但是我不知道如何渲染对象数组.我想可以将每个嵌套数组分配给自己的变量,但是它们可以有任意数量,因此我确实需要将它们保留为数组.

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.

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

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.

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}}

我很确定{{name}}不属于其中,但是我不知道还有什么.

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

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

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]

我不能使用该字符串作为json对象,它只是一个字符串.

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

我对骨干和胡须都比较陌生,所以我认为某人可能对此有最佳实践"解决方案,或者至少可以告诉我是否可行,因此我不再浪费时间了.我在互联网上的任何地方都找不到类似的问题.

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.

推荐答案

这个问题已有2年历史了,但我想迟到总比没有好.您可以使用{{.}}引用数组中的当前元素.

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

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}}
"

这篇关于如何用胡子渲染对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆