-
-
Notifications
You must be signed in to change notification settings - Fork 870
Encourage decorator-style Ember.computed/Ember.observer #110
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,11 +104,11 @@ var president = Ember.Object.create({ | |
firstName: "Barack", | ||
lastName: "Obama", | ||
|
||
fullName: function() { | ||
fullName: Ember.computed(function() { | ||
return this.get('firstName') + ' ' + this.get('lastName'); | ||
|
||
// Call this flag to mark the function as a property | ||
}.property() | ||
}) | ||
}); | ||
|
||
president.get('fullName'); | ||
|
@@ -124,15 +124,18 @@ You can tell Ember about these dependencies like this: | |
|
||
```javascript | ||
var president = Ember.Object.create({ | ||
firstName: "Barack", | ||
lastName: "Obama", | ||
init: function() { | ||
this._super.apply(this, arguments); | ||
this.set('firstName', 'Barack'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this kind of initialization encouraged? I thought that the shape deoptimization happened when defining attributes at a class level (using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fwiw, Babel make it easy to use super anyway: init() {
this._super(...arguments);
// some thing
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't that be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know nothing about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. most runtimes special case Also, no arguments isn't going away in ES2015. And, properties set in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. arguments is "deprecated" in ES2015, Babel handles this fine: function bob(...args) {
return this.fmt(...args);
} function bob() {
return this.fmt.apply(this, arguments);
} More in depth explanation why I put that in quotes: https://esdiscuss.org/notes/2015-03-25 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the alternative I linked to above now shows the ideal behavior as @thejameskyle describes. Not sure if I read the output incorrectly or if there was a deploy of the repl. So, using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thejameskyle Ya, but its not going away in our life times. Except in potentially different modes, or new contexts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it is actually deprecated. I think those comments may have mislead you. |
||
this.set('lastName', 'Obama'); | ||
}, | ||
|
||
fullName: function() { | ||
fullName: Ember.computed('firstName', 'lastName', function() { | ||
return this.get('firstName') + ' ' + this.get('lastName'); | ||
|
||
// Tell Ember that this computed property depends on firstName | ||
// and lastName | ||
}.property('firstName', 'lastName') | ||
// Tell Ember that this computed property depends on firstName | ||
// and lastName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
}) | ||
}); | ||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,19 +27,26 @@ of the interval. Since the `tick` method observes the incremented property | |
another interval is triggered each time the property increases. | ||
|
||
```app/services/clock.js | ||
var observer = Ember.observer; | ||
var on = Ember.on; | ||
|
||
export default Ember.Object.extend({ | ||
pulse: Ember.computed.oneWay('_seconds').readOnly(), | ||
tick: function () { | ||
var clock = this; | ||
Ember.run.later(function () { | ||
var seconds = clock.get('_seconds'); | ||
if (typeof seconds === 'number') { | ||
clock.set('_seconds', seconds + (1/4)); | ||
} | ||
}, 250); | ||
}.observes('_seconds').on('init'), | ||
_seconds: 0 | ||
}); | ||
init: function() { | ||
this._super.apply(this, arguments); | ||
this._seconds = 0; | ||
}, | ||
|
||
pulse: Ember.computed.oneWay('_seconds').readOnly(), | ||
tick: on(observer('_seconds', function () { | ||
var clock = this; | ||
Ember.run.later(function () { | ||
var seconds = clock.get('_seconds'); | ||
if (typeof seconds === 'number') { | ||
clock.set('_seconds', seconds + (1/4)); | ||
} | ||
}, 250); | ||
}), 'init') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. http://emberjs.com/api/classes/Ember.html#method_on isn't this backward? Shouldn't it be |
||
}); | ||
``` | ||
|
||
### Binding to the `pulse` attribute | ||
|
@@ -69,20 +76,20 @@ a handful of properties used as conditions in the Handlebars template. | |
|
||
```app/controllers/interval.js | ||
export default Ember.ObjectController.extend({ | ||
secondsBinding: 'clock.pulse', | ||
fullSecond: function () { | ||
return (this.get('seconds') % 1 === 0); | ||
}.property('seconds'), | ||
quarterSecond: function () { | ||
return (this.get('seconds') % 1 === 1/4); | ||
}.property('seconds'), | ||
halfSecond: function () { | ||
return (this.get('seconds') % 1 === 1/2); | ||
}.property('seconds'), | ||
threeQuarterSecond: function () { | ||
return (this.get('seconds') % 1 === 3/4); | ||
}.property('seconds') | ||
}); | ||
secondsBinding: 'clock.pulse', | ||
fullSecond: Ember.computed('seconds', function () { | ||
return (this.get('seconds') % 1 === 0); | ||
}), | ||
quarterSecond: Ember.computed('seconds', function () { | ||
return (this.get('seconds') % 1 === 1/4); | ||
}), | ||
halfSecond: Ember.computed('seconds', function () { | ||
return (this.get('seconds') % 1 === 1/2); | ||
}), | ||
threeQuarterSecond: Ember.computed('seconds', function () { | ||
return (this.get('seconds') % 1 === 3/4); | ||
}) | ||
}); | ||
``` | ||
|
||
A controller for a list of comments, each comment will have a new clock | ||
|
@@ -92,26 +99,26 @@ comment was created. | |
|
||
```app/controllers/comment-item.js | ||
export default Ember.ObjectController.extend({ | ||
seconds: Ember.computed.oneWay('clock.pulse').readOnly() | ||
seconds: Ember.computed.oneWay('clock.pulse').readOnly() | ||
}); | ||
``` | ||
|
||
```app/controllers/comments.js | ||
import ClockService from '../services/clock'; | ||
|
||
export default Ember.ArrayController.extend({ | ||
itemController: 'commentItem', | ||
comment: null, | ||
actions: { | ||
add: function () { | ||
this.addObject(Em.Object.create({ | ||
comment: this.get('comment'), | ||
clock: ClockService.create() | ||
})); | ||
this.set('comment', null); | ||
} | ||
itemController: 'commentItem', | ||
comment: null, | ||
actions: { | ||
add: function () { | ||
this.addObject(Em.Object.create({ | ||
comment: this.get('comment'), | ||
clock: ClockService.create() | ||
})); | ||
this.set('comment', null); | ||
} | ||
}); | ||
} | ||
}); | ||
``` | ||
|
||
### Handlebars template which displays the `pulse` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment isn't accurate now.