View的事件没有触发

时间:2012-07-20 14:23:29

标签: backbone.js

我使用以下视图。问题是#likeCar的点击事件没有被解雇。我在这里做错了什么?

window.LikeView = Backbone.View.extend({
    events: {
        "click #likeCar":"likeCar"
    },

    initialize : function(){
        this.el = $("#liker");
        this.template = _.template($('#like-template').html());
        //_.bindAll(this,"render");
        this.model.bind("change", this.render, this);
    },

    render : function (eventName) {
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },

    likeCar : function(e) {
        console.log("like car")
    }
});

模板:

<script type="text/template" id="like-template">
<a id="likeCar" class="btn btn-mini" href="#">Like</a>

1 个答案:

答案 0 :(得分:1)

尝试在视图定义中包含此行:

el: $('#liker')

而不是在initialize方法中初始化它。

似乎在这里工作:

http://jsfiddle.net/xqREW/6/