简体   繁体   English

向backbonejs中的ul添加一个集合

[英]Adding a Collection to a ul in backbonejs

I'm just learning js/backbonejs and I have a simple question, Please feel free to direct me to a duplicate.我只是在学习 js/backbonejs,我有一个简单的问题,请随时指导我复制。

I have a Collection, I've populated it and I can access it in the console by doing the standard:我有一个集合,我已经填充了它,我可以通过执行标准在控制台中访问它:

collection1.at(1).get('name');

I can also loop through the values by doing:我还可以通过执行以下操作来遍历值:

for(vars i = 0; i < collection1.size(); i++)
{
console.log(collection1.at(i).get('name'));
}

I have four buttons and have views on them and functions that correctly output something to the console when i click on them.我有四个按钮,并且有它们的视图和功能,当我单击它们时,可以将某些内容正确地输出到控制台。 When i click on the Show all button i want to display every model in the collection along with the data it has (id,name,fame);当我点击 Show all 按钮时,我想显示集合中的每个模型及其拥有的数据(id、name、fame);

How would i go about doing this?我该怎么做呢? I know i have to have a我知道我必须有一个

<ul id = "gottaChangeThis"></ul>

How would I be able to add something like this to it:我怎么能添加这样的东西:

<li><%=id%><%=name%><%=fame%></li>

Any help or redirection would be helpful, Thanks任何帮助或重定向都会有帮助,谢谢

The basic architecture could include a Backbone.View that accepts your Collection.基本架构可以包括一个接受您的集合的 Backbone.View。 On render, iterate through the Models in the collection, and for each one render a different Backbone.View (to render the <li> ) and append it to the parent <ul> element.在渲染时,遍历集合中的模型,并为每个模型渲染不同的 Backbone.View(以渲染<li> )并将其附加到父<ul>元素。

As an alternative, consider using Marionette .作为替代方案,请考虑使用Marionette It's a Backbone framework/extension that gives you additional objects as a means to eliminate a lot of boilerplate.它是一个 Backbone 框架/扩展,它为您提供额外的对象作为消除大量样板的一种手段。 In your case, you'd want a Marionette.CollectionView with a childView specified.在您的情况下,您需要指定 childView 的Marionette.CollectionView This childView could be a Marionette.ItemView, such that when you render the CollectionView, it automagically instantiates and renders a childView for each Model in your Collection.这个 childView 可以是 Marionette.ItemView,这样当你呈现 CollectionView 时,它会自动实例化并为你的 Collection 中的每个模型呈现一个 childView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM