Backbone.js事件绑定未触发

时间:2012-10-17 15:32:22

标签: javascript backbone.js backbone-events

select从模板呈现,在调用renderGroups()之前不存在于DOM中。

我认为事件与视图绑定所以如果首先在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

http://jsfiddle.net/BDgMu/1/

1 个答案:

答案 0 :(得分:0)

看起来$("#groups")不在DirectoryView.el内。我认为$('#groups')select