灰烬isController V /秒isRoute V /秒isComponent

问题描述:

我有一个方法,一个共同的混入来自不同的地方(路线/控制器/组件)触发灰烬isController V /秒isRoute V /秒isComponent

我的问题是什么是准确地识别“上下文的最佳方法这个(即,是否该呼叫被从一个路径/控制器/成分产生)

我有此

isRoute: Ember.computed('target', function() { 
     const isUndefined = typeof this.get('target') === 'undefined' 
     return isUndefined ? true : false 
    }), 

    isController: Ember.computed('target', function() { 
     const isUndefined = typeof this.get('target') === 'undefined' 
     return isUndefined ? false : true 
    }), 

然而,即使对于该组件,所述isController返回true。所以它不是唯一的识别。

我需要一种精确的方法来唯一地标识的所有3

而是做的事情,这样,我会建议对不同的地方不同的混入,然后通过其他对象共享混入之间的公共代码。允许您在不将参数代码混合在一起的情况下完成自己的工作。

你可以接近这样

../Mixin.js

Ember.Mixin.create({ 
    actions :{ 

     identifyFromLocation: function(isFromController, isFromRoute, isFromComponent) { 
      if (isFromController) { 
       //Do Process here if from controller 
      } 
      if (isFromModel) { 
       //Do Process here if from Model 
      } 
      if (isFromComponent) { 
       //Do Process here if from component 
      } 
     } 
    } 
}); 

../route.js

Ember.Route.extend({ 
    actions: { 
     sendToMixin() : function() { 
      this.send('identifyFromLocation', false, true, false) 
     } 
    } 
}); 

../component.js

Ember.Component.extend({ 
    actions: { 
     sendToMixin() : function() { 
      this.send('identifyFromLocation', false, false, true) 
     } 
    } 
}); 

../controller.js

Ember.Controller.extend({ 
    actions: { 
     sendToMixin() : function() { 
      this.send('identifyFromLocation', true, false, false) 
     } 
    } 
}); 
+0

谢谢你。我可以问我的方式是否是路由器,isController ...是不是一种安全的识别方式。你看到的任何问题? – testndtv

+0

另外我如何触发sendToMixin在3个地方的每一个? – testndtv

+0

http://*.com/questions/27375856/using-mixins-with-ember-cli/27378249#27378249 –

我觉得relativly保存方法来验证,如果事情是控制器只是使用instanceof

if(this instanceof Ember.Controller) {...} 

这也应该航线&组件协同工作。

+0

Thx很多..这也将它也唯一确定组件? – testndtv

+0

@testndtv肯定! – Lux