[英]Handlebars access an [object object]?
So inside a model I have a structure like this. 因此,在模型内部,我具有这样的结构。 Below is a sample of the structure.
下面是该结构的示例。
var myModel {
_id: 798698,
username: "John",
message: {
message1: "Some cool messsage",
message2: "I'm mad Ohio State lost"
}
}
I am using backbone marionette and handlebars of course, I have a collection view which at first was just a collection of the models. 我使用的是骨架木偶和车把,当然,我有一个收藏视图,起初只是模型的一个收藏。
var Marionette = require('backbone.marionette');
var ChatView = Marionette.ItemView.extend({
template: require('../../templates/message.hbs')
});
module.exports = CollectionView = Marionette.CollectionView.extend({
initialize: function() {
this.listenTo(this.collection, 'change', this.render);
},
itemView: ChatView
});
Then in the /message.hbs
the template is basic like this. 然后在
/message.hbs
,模板是基本的,就像这样。
<div class="message"><b>{{username}}:</b>{{message}}</div>
Well if you look at the structure above message
is obviously an [object object]
. 好吧,如果您看一下上面的结构,
message
显然是[object object]
。
So to the question which I am sure is easy and obvious, how do I loop this view for each object inside the message:
attribute, so that the template itself is looped. 因此,对于我确定很容易理解的问题,如何为
message:
属性中的每个对象循环此视图,以便使模板本身循环。 I think I worded that right. 我想我说的没错。
See ideally It would be nice to create a collection within the model, but I didn't know how and the objects inside message:
are built in a vanilla way. 理想情况下查看在模型中创建一个集合会很好,但是我不知道
message:
内部的方式和对象message:
是以香草方式构建的。
So that said I am hoping there is a way to loop those objects with backbone/backbone.marionette and handlebars. 因此,我希望有一种方法可以使这些对象与骨干/backbone.marionette和车把循环。
Thanks 谢谢
In your template, use #each: 在模板中,使用#each:
<div class="message">
<b>{{username}}:</b>
{{#each message}}
<div>{{this}}</div>
{{/each}}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.