2016-07-11 13 views
1

を解決した後、私はいくつかの機能がアクションで定義されているサードパーティのコンポーネントを拡張していると私は私のサブクラスでそのアクションをキャッチしたい、Aを呼び出すエンバー2.6.0EmberJS - 約束は

を使用していますスーパーアクションを呼び出します約束を返し、約束が解決したらsupersの行動を起こす関数。

ので、サードパーティのコンポーネントは、この行います

import Ember from 'ember'; 

export default Ember.Component.extend({ 
    actions: { 
    theAction() { 
     this._somePrivateFunction(); 
     //do other stuff 
    } 
    } 
}); 

そして、私のサブクラスでは、私がやっている:約束のコールバックで呼び出されたとき

import Ember from 'ember'; 
import ThirdPartyComponent from 'path/to/component' 

export default ThirdPartyComponent.extend({ 
    _funcThatReturnsPromise() { 
    return new Ember.RSVP.Promise(); 
    } 
    actions: { 
    theAction() { 
     const thePromise = this._funcThatReturnsPromise(); 
     thePromise.then(() => { 
      // undefined! 
      this._super(...arguments); 
     }) 
    } 
    } 
}); 

this._super()は、親コンポーネントのアクションに解決されませんが。私はプロパティとしてスーパークラスの機能を格納しようとしたし、それを呼び出しました:

theAction() { 
      const thePromise = this._funcThatReturnsPromise(); 
      this.set('superFunc', this._super); 
      thePromise.then(() => { 
      // calls the super but "this" inside the supers action is undefined 
      this._super(...arguments); 
     }) 
    } 

これ、醜いことに加えて、スーパークラスのアクション内部thisの結果は未定義されます。私はそれがなぜいくつかのドキュメントを見ているのか分かりません。

私のサブクラスでアクションsend()を呼び出すためのオプションもあります:関数は自分自身を呼び出して終了するので、無限ループでコースの結果の

theAction() { 
     const thePromise = this._funcThatReturnsPromise(); 
     this.set('superFunc', this._super); 
     thePromise.then(() => { 
      //infinite loop! 
      this.send('theAction'); 
     }); 
    } 

しかし、これは。

ここに進む方法がわかりません。私がここでやろうとしていることをするためのきれいな方法があれば教えてもらえますか?アドバイスをいただければ幸いです。どうもありがとう!子コンポーネントで

+0

'箱から出して動作します。 – Bergi

+0

@Bergiはい、理想的なケースですが、Emberjsコンポーネントの範囲内で作業しています:-( – TheMethod

答えて

0

が好きですか:あなたは本当のES6 `class`、` super.theAction(...)を使用していた場合は

theAction() { 
     const thePromise = this._funcThatReturnsPromise(); 
     let parentAction = this._super; 
     let self = this; 
     thePromise.then(() => { 
      //parent usage 
      parentAction(); 

      // this usage 
      self.doSome(); 
     }); 
    } 
+0

@ Ebrahim Pasbani上記の私の質問で私は既にこれを試しました。この方法では – TheMethod

+0

@TheMethodいいえ、何かを試しました。間違っているコンポーネントのプロパティとして設定しました –

+0

返信ありがとうございます。プロパティまたは関数スコープの変数に 'this._super'を割り当てるかどうか – TheMethod