Skip to content

Instantly share code, notes, and snippets.

@ef4
Created September 14, 2011 23:13
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 ef4/1218076 to your computer and use it in GitHub Desktop.
Save ef4/1218076 to your computer and use it in GitHub Desktop.
SC.ShowHelper = SC.View.extend
# We bind to either of these values...
if: true
unless: false
# ... to control visibility.
shouldDisplay: (->
@get('if') and not @get('unless')
).property('if', 'unless')
# We're using a span for consistency with "#if". But if you want to
# embed block-level content, you should override this with 'div'.
tagName: 'span'
# This lets the body of our template refer direct to our enclosing
# context.
templateContext: (->
@getPath('parentView.templateContext')
).property('parentView')
# Keep the 'style' attribute up to date to enforce our visibility.
attributeBindings: ['style']
style: (->
if @get('shouldDisplay')
''
else
'display: none'
).property('shouldDisplay')
# Common boilerplate for our helpers.
helper = (implementation) -> (path, options) ->
sc_assert("You must pass exactly one argument to the showIf/showUnless helpers", arguments.length == 2)
sc_assert("You must pass a block to the showIf/showUnless helpers",
options.fn && options.fn != Handlebars.VM.noop)
implementation(path, options)
SC.Handlebars.ViewHelper.helper(this, 'SC.ShowHelper', options)
# Register helpers with Handlebars
Handlebars.registerHelper 'showIf', helper (path, options) ->
options.hash.ifBinding = path
Handlebars.registerHelper 'showUnless', helper (path, options) ->
options.hash.unlessBinding = path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment