this。$ el.off不是函数

Jes*_*lly 1 javascript jquery backbone.js

以前可以使用,但是我已将下划线和主干更新为最新版本,然后出现以下错误:

Uncaught TypeError: this.$el.off is not a function
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/mmm770v8/

  SearchView = Backbone.View.extend({
        initialize: function(){
            this.render();
        },
        render: function(){
            var template = _.template( $("#search_template").html(), {} );
            this.el.html( template );
        },
        events: {
            "click input[type=button]": "doSearch"  
        },
        doSearch: function(){
            // Button clicked
            console.log(this.el.find('#search_input').val()); 
        }
    });
Run Code Online (Sandbox Code Playgroud)

mu *_*ort 5

您有几个问题:

  1. 您的小提琴使用的是古老的jQuery 1.5.2,并使用bind/ unbind代替on/ off。Backbone期望具有onoff功能的jQuery的更新版本。

  2. 您使用的this.el是您的意思this.$elthis.el只是一个普通的旧DOM节点,this.$el就是cached $(this.el)

  3. var html = _.template(tmpl, data)形式_.template 在下划线1.7.0走了。您现在需要两步过程:

    var t = _.template(tmpl);
    var h = t(data);
    
    Run Code Online (Sandbox Code Playgroud)

    所以你render应该看起来像这样:

    render: function() {
        var template = _.template($("#search_template").html());
        this.$el.html(template({}));
    }
    
    Run Code Online (Sandbox Code Playgroud)

更新小提琴:http : //jsfiddle.net/ambiguous/L5z4agh4/