On Github cowboyd / emberatx-2013-02-28
Created by Charles Lowell / @cowboyd
Are all letters created equal?
No! Get your model right, and everything else comes easily.
(Ember is guilty of this)
MVC is Kung Fu. There are many different styles
Ember's is strong
(computed or otherwise)
Circle = Ember.Object.extend area: (-> r = get 'r' Math.PI * r * r ).property 'r' c = Circle.create(r: 1) c.get 'area' # => 3.141592653589793 c.set 'r', 2 c.get 'area' # => 12.566370614359172
c = Circle.create(r: 1) c.addObserver 'area', -> console.log "area changed to #{c.get 'area'}" c.set 'r', 2
area changed to 12.566370614359172
Drag = Ember.Object.extend path: [] start: (-> @get('path.firstObject') || Point.create() ).property 'path.firstObject' end: (-> @get('path.lastObject') || Point.create() ).property 'path.lastObject' delta: (-> x: @get('end.x') - @get('start.x') y: @get('end.y') - @get('start.y') ).property 'start', 'end' Drag.Interaction = Ember.Object.extend init: -> #... $(document.body).on 'mousemove', (e)=> @get('drag.path').pushObject Point.create x: e.pageX, y: e.pageY
Drag = Ember.Object.extend #... offset: left: @get('offset0.left') + @get('delta.x') top: @get('offset0.top') + @get('delta.y') ).property 'delta.x', 'delta.y', 'offset0.left', 'offset0.top' Drag.Interaction = Ember.Object.extend #... updateOffsets: (-> $(@get 'element').css left: @get 'drag.offset.left' top: @get 'drag.offset.top' ).observes 'drag.offset.left', 'drag.offset.top'
Models based on computed properties outnumber all other classes by at least 10:1
Only use observers where the surface of your model makes contact with the DOM