簡體   English   中英

Backbone.js事件綁定未觸發

[英]Backbone.js Event Binding not firing

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/

看起來$("#groups")不在DirectoryView.el 我想$('#groups')select

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM