是从模板呈现的select
,并且在被调用之前不存在于 DOMrenderGroups()
中。
我认为这些事件是绑定到视图的,所以如果选择一开始在 DOM 中不存在并不一定重要?
我是新手,backbone.js
所以它可能很明显:
var DirectoryView = Backbone.View.extend({
el: $("table tbody"),
initialize: function(){
// Populate our collection
this.collection = new Directory(contacts);
// Get our unique groups
var groups = this.getGroups();
this.renderGroups(groups);
},
renderGroups: function(groups) {
var group = $("#groups");
var groupView = new GroupView({
model: {groups: groups}
})
group.append(groupView.render().el)
},
events : {
"change select" : "select"
},
select: function(e) {
console.log("hello")
},
getGroups: function() {
// Pluck all the group properties from our array of contacts and return the unique groups
return _.uniq(this.collection.pluck("group"), false, function(group){
return group.toLowerCase();
});
}
});
模板:
<script id="group-option-tmpl" type="text/template">
<option value="all">All</option>
<% _.each(groups, function(group) { %>
<option value="<%= group %>"><%= group %></option>
<% }); %>
</script>
JSFiddle