Last active
December 21, 2015 12:48
-
-
Save ssoroka/6307856 to your computer and use it in GitHub Desktop.
a "method_missing" example in ember.js. Catch setting undefined properties and serialize them to an "optionsString" property as a string of JSON.
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.User = DS.Model.extend | |
name: DS.attr 'string' | |
email: DS.attr 'string' | |
# dynamically defined properties are serialized as a string of JSON here: | |
optionsString: DS.attr 'string' | |
# optionsData lets us deal with the string as a JSON object | |
optionsData: ((k,v) -> | |
if arguments.length == 2 | |
@set('optionsString', JSON.stringify(v)) | |
v | |
else | |
JSON.parse(@get('optionsString') || '{}') | |
).property('optionsString') | |
# our dynamic "method_missing" getter | |
unknownProperty: (k) -> | |
@get('optionsData')[k] | |
# our dynamic "method_missing" setter | |
setUnknownProperty: (k, v) -> | |
if k.match /(store|_reference)/ # careful not to store these objects! | |
@[k] = v | |
else | |
o = @get('optionsData') | |
o[k] = v | |
@set('optionsData', o) | |
o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment