【发布时间】:2014-05-07 18:57:19
【问题描述】:
这是我找到的另一个帖子的扩展:Backbone: A list of views inside a view
我很难理解这一点(我以前从未使用过下划线),所以我认为最好发布一个关于此的新主题。
场景
我有一个创建并列出各种信息的群聊模型。这包括该群聊中的参与者列表。我需要将该列表显示为正在渲染的模型的一部分。
代码
创建参与者数组
var participants = [];
$(iq).find('participants').each(function() {
var participantsNodes = this.childNodes;
for (var i = 0; i < participantsNodes.length; i++) {
var participantAttr = participantsNodes[i].attributes
var participant = participantAttr[0].nodeValue;
participants.push({"name": participant, "chatid": chatid});
}
});
...
model.set({
participants : participants,
chatid : chatid
...
});
ItemView
GroupChatView = Backbone.Marionette.ItemView.extend({
template : "#template-groupchat",
tagName : 'div',
className : 'alert alert-custom',
initialize: function(){
this.$el.prop("id", this.model.get("chatid"));
},
...
正在使用的模板
<script type="text/template" id="template-groupchat">
<a id='close-<%= chatid %>' class='close hide' data-dismiss='alert' href='#'>
×
</a>
...
<div class='row' id='row-participants-<%= chatid %>' style='display: none'>
<!-- CUSTOM CODE SHOULD BE HERE? My attempt:
<% _.each(participants, function () { %>
<%= name %>
<% }); %>
-->
</div>
</script>
这会不断返回一个错误,指出“名称”不存在。我不确定我在原始帖子中做错了什么,我认为我将 participants 数组错误地传递给了 underscore.each() 部分。
【问题讨论】:
标签: javascript backbone.js underscore.js marionette