ajputnam ajputnam - 4 months ago 17
Javascript Question

Where should conditional action logic go?

I am working on a project that uses ember. Conditional logic can be placed in the HTML template, the controller and where the function is define. I have an action that only needs to be fired when a global variable is true.

<div {{action 'CallThisAction' on='click'}}>...</div>


In the .hbs I can do

{{#if global.x}}
<div {{action 'CallThisAction' on='click'}}>...</div>
{{else}}
<div>...</div>
{{/if}}


or in the function I can

CallThisAction(){
if(global.x){
//do something
}
}


or I could add logic into the controller to prevent the CallThisAction from being called biased off the global.x. Controllers are also depreciated.

I could also pass as param:

CallThisAction(x){
if(x){
//do something
}
}


Is there a cleaner way to do this? Ideally the solution here would be nice:Feature Request

Answer

Your template shouldn't use global variables. It's better to store such logic in your action:

actions: {
  CallThisAction(){
    if(!global || !global.x){
      return;
    }
    // do something
  }
}
Comments