骨干继承不能访问子进程中的子进程

时间:2012-01-17 17:15:23

标签: backbone.js

我目前正在使用backbone.js,我有一个奇怪的问题:

我有一个班级:

var AppView = Backbone.View.extend({
...
    events: {
        // Score bar
        "mouseover #jauge-bottom": "showToolTip",
        "mouseleave #jauge-bottom": "hideToolTip",
        // Form events and inputs
        "click a.social": "socialAction",
        "click a#sound": "switchMute",
        "submit form#form-intro": "introForm",
        "click .choisir-perso a.btn-jouer": "sexSet",
        "mouseover .overlay": "sexHover",
        "click #male": "sexClick",
        "mouseover #male": "sexHover",
        "click #female": "sexClick",
        "mouseover #female": "sexHover",
        "mouseleave #male": "sexHover",
        "mouseleave #female": "sexHover",
        "click input.field": "inputFocus",
        "blur input.field":  "inputFocus"
    },

    initialize: function() {
        this.user.bind('change', this.setScore, this);
      _.bindAll(this, 'render', 'setScore');
    },

一个子类:

var Level1 = AppView.extend({

    events: _.extend({
    }, appMainView.prototype.events), // set the inheritance events

    render: function(){
        this.bla();
    },

    bla: function(){
        console.log('bla');
    }
});

我在控制台中收到此错误:

this.bla is not a function

我的问题是为什么以及如何解决?

1 个答案:

答案 0 :(得分:3)

您收到错误,因为render函数上下文未设置为视图。

添加initialize函数以将渲染函数绑定到视图。

var Level1 = AppView.extend({
  initialize: function() {
    _.bindAll(this, 'render');
  },

  events: _.extend({
  }, appMainView.prototype.events), // set the inheritance events

  render: function(){
    this.bla();
  },

  bla: function(){
    console.log('bla');
  }
});