Skip to content
This repository has been archived by the owner on May 26, 2019. It is now read-only.

Commit

Permalink
some misc formatting cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed Apr 1, 2015
1 parent 8e76296 commit 45f5865
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ var Router = Ember.Router.extend({
});

Router.reopen({
notifyGoogleAnalytics: function() {
notifyGoogleAnalytics: Ember.on('didTransition', function() {
return ga('send', 'pageview', {
'page': this.get('url'),
'title': this.get('url')
});
}.on('didTransition')
})
});

export default Router;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ to the text field by accessing the components's jQuery `$` property:

```app/components/focus-input.js
export default Ember.TextField.extend({
becomeFocused: function() {
becomeFocused: Ember.on('didInsertElement', function() {
this.$().focus();
}.on('didInsertElement')
})
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,25 @@ 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({
init: function() {
this._super.apply(this, arguments);
this._seconds = 0;
},

pulse: Ember.computed.oneWay('_seconds').readOnly(),
tick: Ember.observer('_seconds', function () {
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);
}).on('init')
}), 'init')
});
```

Expand Down Expand Up @@ -96,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`
Expand Down
20 changes: 14 additions & 6 deletions source/object-model/observers.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ You can set up an observer on an object by using the `observes`
method on a function:

```javascript
var computed = Ember.computed;
var observer = Ember.observer;
var on = Ember.on;

Person = Ember.Object.extend({
// these will be supplied by `create`
firstName: null,
lastName: null,

fullName: Ember.computed('firstName', 'lastName', function() {
fullName: computed('firstName', 'lastName', function() {
var firstName = this.get('firstName');
var lastName = this.get('lastName');

return firstName + ' ' + lastName;
}),

fullNameChanged: Ember.observer(function() {
fullNameChanged: on('init', observer('fullName', function() {
// deal with the change
}).on('init')
}))
});

var person = Person.create({
Expand Down Expand Up @@ -90,17 +94,21 @@ Observers never fire until after the initialization of an object is complete.

If you need an observer to fire as part of the initialization process, you
cannot rely on the side effect of set. Instead, specify that the observer
should also run after init by using `.on('init')`:
should also run after init by using `Ember.on('init')`:

```javascript
var observer = Ember.observer;
var on = Ember.on;

Person = Ember.Object.extend({
init: function() {
this._super.apply(this, arguments);
this.set('salutation', "Mr/Ms");
},

salutationDidChange: Ember.observer('salutation', function() {
salutationDidChange: on('init', observer('salutation', function() {
// some side effect of salutation changing
}).on('init')
}))
});
```

Expand Down

0 comments on commit 45f5865

Please sign in to comment.