2016-06-14 5 views
0

私はmixinで以下のコードを使用します。 &をパラメータ化してメソッドにコード化することができます。そのため、複数回書く必要はありません。最適化/移動を一般的な場所で行う

var routeName = 'some.nested.route'; 
var someCode = 'XYZ'; 
if (this.get('targetObject')) { 
    // This is when coming from component 
    this.get('targetObject').transitionToRoute(routeName, someCode).then(function(newRoute) { 
     newRoute.controller.set('booleanVar', false); 
    }); 
} else { 
    if (typeof this.transitionToRoute == 'function') { 
     // This is when coming from controller 
     this.transitionToRoute(routeName, someCode).then(function(newRoute) { 
      newRoute.controller.set('booleanVar', false); 
     }); 
    } else { 
     // This is when coming from route 
     this.transitionTo(routeName, someCode).then(function(newRoute) { 
      newRoute.controller.set('booleanVar', false); 
     }); 
    }         
} 

は、私がそのメソッドにちょうどrouteName & someCodeを渡すことができると言います。

答えて

0

これは何か?

transitionExec = function() { 
return this.transitionToRoute(routeName, someCode).then(function(newRoute) { 
     newRoute.controller.set('booleanVar', false); 
    }); 
} 
this.get('targetObject').transitionExec(); 
+0

.bind(this)に忘れたり.call(this, arg1, arg2, ...)でそれを呼び出すか、あなたのthis -contextを失うことになるしないでください – testndtv

0

JavaScriptは非常に動的なので、関数で新しい変数を作成してください。実際に私がパラメータrouteName&someCodeとメソッドを持ちたい

let transition = typeof this.transitionToRoute == 'function' ? this.transitionToRoute.bind(this) : this.transitionTo.bind(this); 

transition(routeName, someCode).then(newRoute => { 
    ... 
}); 
関連する問題