Javascript Backbone.js_确保错误
当我想从router类初始化视图时,我遇到了这个错误 错误是: 未捕获的TypeError:对象#没有方法“_ensureElement” BlogFormView:Javascript Backbone.js_确保错误,javascript,backbone.js,backbone-views,backbone-routing,Javascript,Backbone.js,Backbone Views,Backbone Routing,当我想从router类初始化视图时,我遇到了这个错误 错误是: 未捕获的TypeError:对象#没有方法“_ensureElement” BlogFormView: App.BlogFormView = Backbone.View.extend({ el: ".data-form", initialize: function(){ this.template = _.template($("#blog_form_template").html());
App.BlogFormView = Backbone.View.extend({
el: ".data-form",
initialize: function(){
this.template = _.template($("#blog_form_template").html());
this.render();
},
render: function(){
this.$el.html(this.template({blog: this.model.toJSON()}));
return this;
},
events: {
"click .submit-blog" : "submitForm"
},
submitForm: function(ev){
}
});
路由器:
var blog = new App.Blog();
var blogFormView = App.BlogFormView({model: blog});
路由器代码中缺少新关键字:
var blogFormView = new App.BlogFormView({model: blog});
另外,在initialize方法中调用render通常不是最好的方法。我个人会在路由器代码中调用render