Skip to content

Instantly share code, notes, and snippets.

@ryrych
Created February 18, 2014 20:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryrych/9079251 to your computer and use it in GitHub Desktop.
Save ryrych/9079251 to your computer and use it in GitHub Desktop.
Ember.js Dynamic Computed Property Builder (based on http://www.thesoftwaresimpleton.com/blog/2013/08/11/dyanamic-cp/ by Paul Cowan)
Em.computedPropertyBuilder = (that, name, key, fn) ->
Em.defineProperty that, name, Em.computed(->
fn
).property "#{key}"
messages:
details:
company:
placeholder: 'Please provide company name'
helpMessage: 'Remember that working just for money is gonna make you frustrated om nom nom!'
Em.getMessageFromScope = (scope, key, defaultMessage='') ->
if Em.typeOf(Em.t scope) is 'string' and key is 'label'
Em.t scope
else
if Em.t(scope)?[key] then Em.t(key, scope: scope) else defaultMessage
App.SomeView = Em.View.extend
messages: 'placeholder helpMessage'.w()
# passed as a param in view init
scope: 'messages.details.company'
init: ->
@buildMessages()
@_super()
buildMessages: ->
@get('messages').forEach (string) =>
Em.computedPropertyBuilder @, string, 'scope', Em.getMessageFromScope(@get('scope'), string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment