使用Backbone.js从列表中选择元素
Selecting elements from a list with Backbone.js
这里有一个很一般的问题,如果你想让我详细说明,请告诉我。我有一个元素列表,每个元素都由一个视图渲染。单击某个元素时,该元素会高亮显示,负责该元素的视图会将其模型发送到一个单独的视图,该视图会显示与该元素相关的其他信息。
我正在尝试为用户实现使用箭头键在元素之间切换的功能,但我不确定如何做到这一点。
这是我的视图代码:
var app = app || {};
app.EmployeeView = Backbone.View.extend({
tagName: 'li',
className: 'list-group-item',
template: _.template('<%= Name %>'),
render: function() {
var html = this.template(this.model.attributes);
this.$el.html(html);
},
events: {
"mouseover" : function() {
this.$el.addClass('highlighted');
},
"mouseout" : function() {
this.$el.removeClass('highlighted');
},
"click" : function() {
$('.selected').removeClass('selected');
this.$el.addClass('selected');
$(document).trigger('select', [this.model]);
},
},
});
有人对我如何使用箭头键触发适当的视图来转发其模型有什么建议吗?
我有一个非常相似的功能要为我的产品实现。我有一个图片列表。用户单击一个缩略图并将图像显示为全尺寸。在那里,我还有一个显示图片信息的详细框,我希望用户在保持全尺寸视图的同时使用箭头键浏览图片。我还有"上一个"answers"下一个"按钮,它们的工作原理应该相同。我认为结果是清白的,这就是我所做的:
SingleEntryView = Backbone.View.extend({
initialize : function() {
// Here I save a reference to the currentIndex by getting the index
// of the current model within the collection.
this.currentIndex = this.collection.indexOf(this.model);
// Important: here I attach the keydown event to the whole document,
// so that the user doesn't necessarily have to focus the view's
// element to use the arrows.
$document.on('keydown.singleEntry', this.onKeyDown.bind(this));
},
// Having attached the keydown event to document, there's no autocleaning.
// Overriding remove to detach the listener when the view goes away.
remove : function() {
$document.off('.singleEntry');
Backbone.View.prototype.remove.call(this);
},
// Keydown listener. Interested only in arrow keys.
onKeyDown : function(e) {
if (e.keyCode == 37) // left arrow
this.prev();
else if (e.keyCode == 39) // right arrow
this.next();
return true;
},
// Click events on the prev/next buttons (they behave == to the arrow keys)
events : {
'click #prev' : 'prev',
'click #next' : 'next'
},
// Handlers shared by both prev/next buttons and arrow keys.
// What is the current index? Go to the prev/next one.
// If we are at the start/end, wrap around and go to the last/first.
prev : function() {
this.goTo(this.currentIndex > 0 ? this.currentIndex - 1 : this.collection.length - 1);
return false;
},
next : function() {
this.goTo(this.currentIndex < this.collection.length - 1 ? this.currentIndex + 1 : 0);
return false;
},
goTo : function(i) {
this.model = this.collection.at(i);
this.currentIndex = i;
// App is my Router
App.navigate('app/boards/' + this.collection.id + '/entries/' + this.model.id, {
trigger: true
});
}
}
相关文章:
- WebdriverIO waitForExist()选择元素's选定的选项
- 如何选择元素的第n个子元素
- Angular没有根据模型更新我的选择元素
- jquery使用name from变量按类选择元素
- 如何使用jQuery按数据日期属性选择元素
- 向html选择元素添加选项
- 使用Jquery在相同类型的元素中选择元素
- 他们是如何使用angular/jqLite find()方法按属性名称和值选择元素的?ng conf 2015
- 如何在Angular中清除选择元素中的过滤器
- 使用其中一个元素作为参考点,从Javascript数组中选择元素
- Javascript选择元素到字符串的比较
- Angular始终选择选择元素中的第一个选项
- 使用 jquery .find() 遍历按类和存储属性选择元素
- 通过 id json, jquery 选择元素
- 如何基于另一个已经存在的选择器选择元素
- 禁用多个选择元素中的非“选定”选项,但使用 jquery 的一个除外
- HTML 选择元素的只读等效项
- 在iPad上,如何通过Javascript事件在选择元素上设置focus()而不显示选项
- 单击事件似乎不适用于 IE 中的选择元素
- Angular js,在选择元素上,我想 POST 数据以将其保存在数据库中,然后我想使用 PUT 更新它而无需重新加载