Skip to content

Instantly share code, notes, and snippets.

@aortbals
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aortbals/245f05c2105f92768d04 to your computer and use it in GitHub Desktop.
Save aortbals/245f05c2105f92768d04 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle',
isAdministrator: false,
notAdministrator: Ember.computed.not('isAdministrator')
});
import Ember from 'ember'
export function not(params) {
return !params[0]
}
export default Ember.HTMLBars.makeBoundHelper(not)
import Ember from 'ember';
var Router = Ember.Router.extend({
location: 'none'
});
Router.map(function() {
});
export default Router;
<h1>Arbitrary Expressions in HTMLBars</h1><br><br>
<p>
You cannot have arbitrary expressions in HTMLBars/Handlebars such as <code>!</code>. There are two good approaches to this problem, a computed macro or a <code>not</code> helper.
</p>
<p>
Toggle isAdministrator? {{input type='checkbox' checked=isAdministrator}} (value: <code>{{isAdministrator}}</code>)
</p>
<p>
<h3>Take 1: Ember computed macro</h3>
<p>This uses a computed macro called <code>notAdministrator</code> that is the inverse of <code>isAdministrator</code>. See the Controller where it is defined.</p>
<input type='checkbox' disabled={{notAdministrator}}>
<button disabled={{notAdministrator}}>Am I an admin?</button>
</p>
<p>
<h3>Take 2: <code>not</code> helper</h3>
<p>This uses a helper called <code>not</code>. It makes it easy to compose these type of expressions in your template. See <a href='https://github.com/jmurphyau/ember-truth-helpers'>ember-truth-helpers</a>.</p>
<input type='checkbox' disabled={{not isAdministrator}}>
<button disabled={{not isAdministrator}}>Am I an admin?</button>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment