我在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 :(得分:0)
这样的东西?
transitionExec = function() {
return this.transitionToRoute(routeName, someCode ).then(function(newRoute) {
newRoute.controller.set('booleanVar', false);
});
}
this.get('targetObject').transitionExec();
答案 1 :(得分:0)
Javascript非常动态,所以只需使用您的函数创建一个新变量。不要忘记.bind(this)
或使用.call(this, arg1, arg2, ...)
进行调用,否则您将失去this
- 背景:
let transition = typeof this.transitionToRoute == 'function' ? this.transitionToRoute.bind(this) : this.transitionTo.bind(this);
transition(routeName, someCode).then(newRoute => {
...
});