gunn / ember-auto

Nicer computed properties - less this.get("myField")
43 stars 2 forks source link

Reduce repitition #1

Open alexspeller opened 11 years ago

alexspeller commented 11 years ago

This is a bit of a horrible implementation but it's less duplication in use and minification safe:


Em.superAuto = (keys..., func) ->
  str = func.toString()
  regex = /function\s*\(\s*\)/
  arrayRegex = /\@|\[\]/
  unless str.match(regex)
    throw "It doesn't look like you passed a function with no arguments"
  argNames = for key in keys
    splitKey = key.split arrayRegex
    splitKey[0].camelize()
  str = str.replace(regex, "function(#{argNames.join(', ')})")
  eval("funcWithArgs = #{str}")
  Em.computed keys..., ->
    args = for key in keys
      @get key.split(arrayRegex)[0] 
    funcWithArgs(args...)

jsbin

gunn commented 11 years ago

See the conversation at http://discuss.emberjs.com/t/ember-auto-nicer-properties-with-less-this-get-junk/2896/10