Skip to content

Instantly share code, notes, and snippets.

@ssoroka
Last active December 21, 2015 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssoroka/6307856 to your computer and use it in GitHub Desktop.
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.
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