【问题标题】:Backbone.js _ensureElement errorBackbone.js _ensureElement 错误
【发布时间】:2013-12-27 14:36:43
【问题描述】:

当我想从路由器类初始化视图时出现此错误。

错误是: Uncaught TypeError: Object # has no method '_ensureElement'

博客表单视图:

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});

【问题讨论】:

    标签: javascript backbone.js backbone-views backbone-routing


    【解决方案1】:

    您在路由器代码中缺少 new 关键字:

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

    另外,在 initialize 方法中调用 render 通常不是最好的主意。我个人只会在路由器代码中调用 render

    【讨论】:

      猜你喜欢
      • 2013-12-12
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 2012-02-10
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多