最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone.js _ensureElement error - Stack Overflow

programmeradmin7浏览0评论

I'm getting this error, when I want to initialize the view from router class.

Error is: Uncaught TypeError: Object # has no method '_ensureElement'

BlogFormView:

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){

    }
});

Router:

var blog = new App.Blog();
var blogFormView = App.BlogFormView({model: blog});

I'm getting this error, when I want to initialize the view from router class.

Error is: Uncaught TypeError: Object # has no method '_ensureElement'

BlogFormView:

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){

    }
});

Router:

var blog = new App.Blog();
var blogFormView = App.BlogFormView({model: blog});
Share Improve this question edited Feb 25, 2016 at 18:51 larrydalmeida 1,6303 gold badges17 silver badges31 bronze badges asked Dec 10, 2013 at 7:17 Emrah AyanogluEmrah Ayanoglu 4662 gold badges6 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 25

You are missing new keyword in router code:

var blogFormView = new App.BlogFormView({model: blog});

Also, it usually isn't best idea to call render inside the initialize method. I personally would just call render inside the router code.

发布评论

评论列表(0)

  1. 暂无评论