Jes*_*lly 1 javascript jquery backbone.js
以前可以使用,但是我已将下划线和主干更新为最新版本,然后出现以下错误:
Uncaught TypeError: this.$el.off is not a function
Run Code Online (Sandbox Code Playgroud)
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)
您有几个问题:
您的小提琴使用的是古老的jQuery 1.5.2,并使用bind/ unbind代替on/ off。Backbone期望具有on和off功能的jQuery的更新版本。
您使用的this.el是您的意思this.$el。this.el只是一个普通的旧DOM节点,this.$el就是cached $(this.el)。
该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/
| 归档时间: |
|
| 查看次数: |
3807 次 |
| 最近记录: |