Last active
August 29, 2015 13:58
-
-
Save valer-cara/9946383 to your computer and use it in GitHub Desktop.
emberjs mistery setter
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.TimeInputComponent = Ember.Component.extend( | |
MAX_TIME: 99 * 60 + 59 | |
didInsertElement: () -> | |
if @get('value') | |
@set('inputValue', @formatValue(@get('value'))) | |
classNames: ['time-input-component'] | |
classNameBindings: ['focused:focused'] | |
digits: [] | |
keyUp: (e) -> | |
code = e.which | |
char = String.fromCharCode(code) | |
return unless 48 <= code <= 57 or code == 8 or code == 9 | |
digits = @get('digits') | |
if code == 8 | |
digits.shift() | |
else if char.match(/[0-9]/) | |
digits.pop() if digits.length >= 4 | |
digits.unshift(char) | |
# Need this to sync the values of the array in the component's this[META_KEY].values | |
@set('digits.0', digits[0]) | |
@set('digits.1', digits[1]) | |
@set('digits.2', digits[2]) | |
@set('digits.3', digits[3]) | |
## | |
# digits has it's own digits[META_KEY].values that are set initially on object creation. | |
# | |
# withouth the 4 set() calls, digits[META_KEY].values don't change, | |
# so doing a get('digits.3') in the template to show it will return `undefined` | |
# | |
# -- what's the answer to a better implementation of this? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment