-
-
Notifications
You must be signed in to change notification settings - Fork 71
Add deprecation guides for observers #1407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
wagenet
wants to merge
3
commits into
ember-learn:main
Choose a base branch
from
wagenet:ember-observable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
--- | ||
title: Deprecation of @ember/object/observable | ||
until: 7.0.0 | ||
since: 6.5.0 | ||
--- | ||
|
||
The `get` and `set` methods from `@ember/object/observable` are deprecated. You should use native JavaScript getters and setters instead. This also applies to all built-in `Ember.Object` descendants. | ||
|
||
### Replacing `.get()` | ||
|
||
Instead of using `.get()`, you can now use standard property access. | ||
|
||
**Before** | ||
|
||
```javascript | ||
import EmberObject from '@ember/object'; | ||
|
||
const person = EmberObject.create({ | ||
name: 'John Doe', | ||
details: { | ||
age: 30 | ||
} | ||
}); | ||
|
||
const name = person.get('name'); | ||
const age = person.get('details.age'); | ||
``` | ||
|
||
**After** | ||
|
||
```javascript | ||
class Person { | ||
name = 'John Doe'; | ||
details = { | ||
age: 30 | ||
}; | ||
} | ||
|
||
const person = new Person(); | ||
|
||
const name = person.name; | ||
const age = person.details.age; | ||
``` | ||
|
||
For nested properties that might be null or undefined, use the optional chaining operator (`?.`): | ||
|
||
```javascript | ||
const street = person.address?.street; | ||
``` | ||
|
||
### Replacing `.set()` | ||
|
||
Instead of using `.set()`, you can now use standard property assignment. | ||
|
||
**Before** | ||
|
||
```javascript | ||
import EmberObject from '@ember/object'; | ||
|
||
const person = EmberObject.create({ | ||
name: 'John Doe' | ||
}); | ||
|
||
person.set('name', 'Jane Doe'); | ||
``` | ||
|
||
**After** | ||
|
||
```javascript | ||
import { tracked } from '@glimmer/tracking'; | ||
|
||
class Person { | ||
@tracked name = 'John Doe'; | ||
} | ||
|
||
const person = new Person(); | ||
|
||
person.name = 'Jane Doe'; | ||
``` | ||
|
||
### A Note on Legacy Computed Properties and Setters | ||
|
||
When working with legacy computed properties, the way you set matters for reactivity. | ||
|
||
#### Updating Plain Properties | ||
|
||
To trigger reactivity (like re-computing a dependent computed property) when changing a plain property, you **must** use the `set` function. A native JavaScript assignment (`person.firstName = 'Jane'`) will change the value but will **not** trigger reactivity. | ||
|
||
```javascript | ||
import EmberObject, { computed, set } from '@ember/object'; | ||
|
||
class Person { | ||
// These properties are NOT tracked | ||
firstName = 'John'; | ||
lastName = 'Doe'; | ||
|
||
@computed('firstName', 'lastName') | ||
get fullName() { | ||
return `${this.firstName} ${this.lastName}`; | ||
} | ||
} | ||
|
||
const person = Person.create(); | ||
console.log(person.fullName); // 'John Doe' | ||
|
||
// You MUST use `set` to update the plain property for the | ||
// computed property to react. | ||
set(person, 'firstName', 'Jane'); | ||
|
||
console.log(person.fullName); // 'Jane Doe' | ||
``` | ||
|
||
#### Updating Computed Properties with Setters | ||
|
||
In contrast, if a computed property is defined with its own setter, you **can** use a native JavaScript assignment to update it. Ember will correctly intercept this and run your setter logic. | ||
|
||
```javascript | ||
import EmberObject, { computed, set } from '@ember/object'; | ||
|
||
class Person { | ||
firstName = 'John'; | ||
lastName = 'Doe'; | ||
|
||
@computed('firstName', 'lastName') | ||
get fullName() { | ||
return `${this.firstName} ${this.lastName}`; | ||
} | ||
set fullName(value) { | ||
const [firstName, lastName] = value.split(' '); | ||
// Note: `set` is still used inside the setter itself | ||
set(this, 'firstName', firstName); | ||
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. set is not imported 🙈 |
||
set(this, 'lastName', lastName); | ||
} | ||
} | ||
|
||
const person = Person.create(); | ||
|
||
// You CAN use a native setter on a computed property with a setter. | ||
person.fullName = 'Jane Doe'; | ||
|
||
console.log(person.firstName); // 'Jane' | ||
console.log(person.lastName); // 'Doe' | ||
``` | ||
|
||
However, for any properties that you use directly in a Glimmer template (`{{this.myProp}}`), you should always use `@tracked` to ensure the template updates when the property changes. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should these examples also have the modern native class equiv? this would help AI scanning later as we want to get rid of EmberObject