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

Commit

Permalink
prefer non prototype extension annotation of observers
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed Mar 27, 2015
1 parent 0b41ddb commit f57cc53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
34 changes: 10 additions & 24 deletions source/object-model/observers.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ Person = Ember.Object.extend({
return firstName + ' ' + lastName;
}),

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

var person = Person.create({
Expand All @@ -40,12 +40,12 @@ is easy to introduce bugs where properties are not yet synchronized:

```javascript
Person.reopen({
lastNameChanged: function() {
lastNameChanged: Ember.observer('lastName', function() {
// The observer depends on lastName and so does fullName. Because observers
// are synchronous, when this function is called the value of fullName is
// not updated yet so this will log the old value of fullName
console.log(this.get('fullName'));
}.observes('lastName')
})
});
```

Expand All @@ -54,9 +54,9 @@ times when observing multiple properties:

```javascript
Person.reopen({
partOfNameChanged: function() {
partOfNameChanged: Ember.observer('firstName', 'lastName', function() {
// Because both firstName and lastName were set, this observer will fire twice.
}.observes('firstName', 'lastName')
})
});

person.set('firstName', 'John');
Expand All @@ -69,9 +69,9 @@ next run loop once all bindings are synchronized:

```javascript
Person.reopen({
partOfNameChanged: function() {
partOfNameChanged: Ember.observer('firstName', 'lastName', function() {
Ember.run.once(this, 'processFullName');
}.observes('firstName', 'lastName'),
}),

processFullName: function() {
// This will only fire once if you set two properties at the same time, and
Expand All @@ -98,9 +98,9 @@ Person = Ember.Object.extend({
this.set('salutation', "Mr/Ms");
},

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

Expand All @@ -118,20 +118,6 @@ observe it so you can update the DOM once the property changes.
If you need to observe a computed property but aren't currently retrieving it,
just get it in your init method.


### Without prototype extensions

You can define inline observers by using the `Ember.observer` method if you
are using Ember without prototype extensions:

```javascript
Person.reopen({
fullNameChanged: Ember.observer('fullName', function() {
// deal with the change
})
});
```

### Outside of class definitions

You can also add observers to an object outside of a class definition
Expand Down
4 changes: 2 additions & 2 deletions source/testing/unit-testing-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ Suppose we have an object that has a property and a method observing that proper
export default Ember.Object.extend({
foo: 'bar',
other: 'no',
doSomething: function(){
doSomething: Ember.observer('foo', function(){
this.set('other', 'yes');
}.observes('foo')
})
});
```

Expand Down

0 comments on commit f57cc53

Please sign in to comment.