Created
February 18, 2014 20:23
-
-
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)
This file contains 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
Em.computedPropertyBuilder = (that, name, key, fn) -> | |
Em.defineProperty that, name, Em.computed(-> | |
fn | |
).property "#{key}" |
This file contains 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
messages: | |
details: | |
company: | |
placeholder: 'Please provide company name' | |
helpMessage: 'Remember that working just for money is gonna make you frustrated om nom nom!' |
This file contains 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
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 |
This file contains 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
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