2012-01-17 123 views
1

我目前正與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 

我的問題是爲什麼以及如何修復它?

回答

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'); 
    } 
}); 
+0

我以爲我已經忘記了一些東西^^ – Awea 2012-01-17 18:32:12