Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js _ensureElement error

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});
like image 982
Emrah Ayanoglu Avatar asked Dec 10 '13 07:12

Emrah Ayanoglu


1 Answers

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.

like image 141
Marian Polacek Avatar answered Oct 18 '22 01:10

Marian Polacek