forked from spine/spine-rails
/
spine.list.js
46 lines (34 loc) · 1.01 KB
/
spine.list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(function(Spine, $){
Spine.List = Spine.Controller.create({
events: {
"click .item": "click"
},
proxied: ["change"],
selectFirst: false,
init: function(){
this.bind("change", this.change);
},
template: function(){ return arguments[0] },
change: function(item){
if ( !item ) return;
this.current = item;
this.children().removeClass("active");
this.children().forItem(this.current).addClass("active");
},
render: function(items){
if (items) this.items = items;
this.el.html(this.template(this.items));
this.change(this.current);
if ( this.selectFirst )
if ( !this.children(".active").length || !this.current )
this.children(":first").click();
},
children: function(sel){
return this.el.children(sel);
},
click: function(e){
var item = $(e.target).item();
this.trigger("change", item);
}
});
})(Spine, Spine.$);