admin管理员组文章数量:1323524
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}}
I am pretty sure {{name}} doesn't belong in there, but I have no idea what else I would put in there.
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]
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.
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}}
I am pretty sure {{name}} doesn't belong in there, but I have no idea what else I would put in there.
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]
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.
Share Improve this question edited Jun 30, 2013 at 22:20 user2455811 asked Jun 30, 2013 at 22:15 user2455811user2455811 1934 silver badges9 bronze badges 2- 1 couldnt you just flatten your array? with underscore´s flatten function? Btw. in there render you should not give back "model.attributes" but "model.toJSON()". – Luke Commented Jun 30, 2013 at 22:33
- @Luke is right, if you're using Mustache then you're going to be doing pretty much all your data wrangling in JavaScript before you get anywhere near the template. – mu is too short Commented Jul 1, 2013 at 1:22
1 Answer
Reset to default 7This 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}}
"
本文标签: javascriptHow to render array of arrays of objects with mustacheStack Overflow
版权声明:本文标题:javascript - How to render array of arrays of objects with mustache - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742131941a2422201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论