Skip to content

Instantly share code, notes, and snippets.

@EndangeredMassa
Created October 6, 2012 19:59
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 EndangeredMassa/3845941 to your computer and use it in GitHub Desktop.
Save EndangeredMassa/3845941 to your computer and use it in GitHub Desktop.
Issues with Ember
###
# This emberjs example makes me cring for a couple reasons,
# but the biggest is that it calls `.property` on the function
# to turn fullName into a auto-updating value.
# Since emberjs is a framework instead of a library, like Backbone,
# it seems like it should do this for you.
App.Person = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
fullName: function() {
return this.get('firstName') +
" " + this.get('lastName');
}.property('firstName', 'lastName')
});
# We can know what `get` calls are made in that context.
# Let's just log those and updated the property value
# when a dependency is changed. I whipped up this quick
# example below.
###
#lib
_ = require 'underscore'
extend = (properties) ->
instance = {}
instance.attributes = {}
getters = {}
dependencies = []
instance.get = (name) ->
this.attributes[name]
instance.set = (name, value) ->
this.attributes[name] = value
for attr in dependencies[name]
this.reset(attr)
instance.reset = (name) ->
this.attributes[name] = getters[name].apply(instance)
for name, func of properties
getters[name] = func
context = _.clone(instance)
context.get = (attrname) ->
dependencies[attrname] ?= []
dependencies[attrname].push(name)
instance.get(attrname)
instance[name] = func.apply(context)
instance
# setup
instance = extend
fullName: ->
firstName = this.get('firstName')
lastName = this.get('lastName')
firstName + ' ' + lastName
# test
instance.set('firstName', 'Some')
instance.set('lastName', 'Guy')
console.log instance.get('fullName') # Some Guy
instance.set('lastName', 'Girl')
console.log instance.get('fullName') # Some Girl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment