Created
September 14, 2011 23:13
-
-
Save ef4/1218076 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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