2012-01-17 13 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