主干-从模板中迭代的集合中获取特定的模型
Backbone - Get a certain model from a collection that is iterated in a template
假设我的模板如下所示:
<script type="text/template" id="template">
{{#each []}}
<div>{{this.id}}</div>
<div>{{this.name}}</div>
<div>{{this.description}}</div>
<input id="my-btn" type="button">Button</input>
{{/each}}
</script>
模型和集合:
var Item = Backbone.Model.extend({
defaults: {
id: null,
name: '',
description: ''
}
});
var ItemCollection = Backbone.Collection.extend({
model: Item,
url: function() {
return '/items';
}
});
在视图中,我只是提取项目并显示它们:
var ItemsView = Backbone.View.extend({
el: $('#items'),
template: Handlebars.compile($('#template').html()),
items: new ItemCollection(),
initialize: function() {
var self = this;
this.items.fetch({success: function() {
self.render();
}});
},
render: function() {
var items = new ItemCollection();
var html = this.template(items.toJSON());
this.$el.html(html);
}
});
模板中每个包含其字段的项目都有自己的按钮。
现在,我需要添加一个功能,该功能将在按下模板中的按钮时触发,因此单击某个功能必须获取与模板中按钮关联的项目。
我该怎么做?
这里有一种方法可以实现您的要求——只需在events
散列中添加一个点击处理程序(http://backbonejs.org/#View-事件)。我为按钮添加了数据短划线属性,以便在单击处理程序中可以确定要对哪个按钮/模型执行操作。
var ItemView = window.Backbone.View.extend({
tagName: 'div',
template: Handlebars.compile($('#template').html()),
events: {
'click .item-btn': 'clickHandler'
},
render: function() {
this.$el.html(this.template({
items: collection.toJSON()
}));
return this; // backbone convention -- return the view
},
clickHandler: function(e) {
alert($(e.target).data().itemId + ' clicked');
}
});
以上视图假设以下模板:
<script type="text/template" id="template">
{{#each items}}
<div>{{this.id}}</div>
<div>{{this.name}}</div>
<div>{{this.description}}</div>
<input class="item-btn" data-item-id="{{this.id}}" type="button" value="Button {{this.id}}"></input>
{{/each}}
</script>
以下是一个工作演示:https://jsfiddle.net/9cgzcc3y/
实现这一点的另一种方法(也是我个人更喜欢的方法)是为每个模型创建一个单独的视图。这样,每个视图都将由一个单独的模型支持。
var ItemView = window.Backbone.View.extend({
tagName: 'div',
template: Handlebars.compile($('#template').html()),
events: {
'click .item-btn': 'clickHandler'
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
},
clickHandler: function() {
alert('clicked: ' + this.model.get('id'));
}
});
var ItemsView = window.Backbone.View.extend({
tagName: 'div',
initialize: function() {
this.collection = collection;
},
render: function() {
var frag = document.createDocumentFragment();
this.collection.each(function(model) {
frag.appendChild(new ItemView({model: model}).render().el);
});
this.$el.append(frag);
return this;
}
});
$('#items').append(new ItemsView().render().$el);
以下是此替代方案的工作演示:https://jsfiddle.net/45kuhp7h/
相关文章:
- 如何使用backbone.js从集合中获取模型名称
- 主干-从模板中迭代的集合中获取特定的模型
- 获取Href的集合
- 尝试从集合中获取特定模型
- Backbone集合:如何从API中获取特定对象以放置在模型中
- 如何获取jQuery集合中的第k个元素..作为JQUERY元素
- 所需响应的主干无限集合获取调用
- Backbonejs最简单的集合获取
- 主干.js:仅添加来自 JSON 集合获取的新模型
- 我无法使用 lodash _.template 从一个主干视图中的多个集合获取数据
- Backbone.js集合获取'这个_byId'未定义
- 使用特定的ajax调用设置集合获取
- 在 Backbone 中使用相同类型的集合获取不同的模型集
- 使用主干集合获取数据子集
- 主干集合获取选项不影响GET请求
- 从backbone.js集合获取model.id使用Tastypie创建成功调用
- Onchange集合获取错误而不是函数
- 主干集合获取抛出id错误
- 从mongodb集合获取所有文档时出错
- 从服务器上的Meteor集合获取项目会抛出“;可以't wait without Fiber”;