Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should Spacebars.call be modified to always return null #21

Open
mitar opened this issue Apr 20, 2015 · 0 comments
Open

Should Spacebars.call be modified to always return null #21

mitar opened this issue Apr 20, 2015 · 0 comments

Comments

@mitar
Copy link
Member

mitar commented Apr 20, 2015

We could add a bit modified version of Spacebars.call which makes sure that all undefined values
are converted to null values. This makes it much easier to work with template inclusions
{{> foobar}}which require inclusion to be a Blaze Template or null, but not undefined.
Additionally, if value is null or undefined, do not complain about not being a function.
This then works much more like CoffeeScript ?, so that {{foo.bar 'test'}} is more like
{{foo?.bar? 'test'}} instead of failing if bar is not defined. In practice, this allows
us to do things like {{> this.lookup.renderEditor}} and {{editor.getValue 'foobar'}}
even if this or editor is undefined.

See meteor/meteor#4230 and meteor/meteor#4232.

This is not a necessary change for Blaze Components to work. But it makes things really simpler sometimes.

Spacebars.call = (value) ->
  if _.isFunction value
    # Evaluate arguments if they are functions (by calling them).
    newArgs = []
    for i in [1...arguments.length]
      arg = arguments[i]
      newArgs.push if _.isFunction(arg) then arg() else arg
    # Always return null instead of undefined.
    value.apply(null, newArgs) ? null

  # If value is not null or undefined, then return it as it is.
  else if value?
    if arguments.length > 1
      throw new Error('Can\'t call non-function: ' + value)
    value

  # But if it is null or undefined, always return null instead.
  else
    null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant