Skip to content

Commit

Permalink
update docs and links
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Sep 28, 2024
1 parent 3a8c7ce commit 25eabbf
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin-ember-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

| Rule | Description | Category ||
| ---- | ----------- | -------- | -- |
| no-create-record-rerender | Helps avoid patterns that often lead to excess or broken renders | 🐞⚡️ ||
| [no-create-record-rerender](./docs/no-create-record-rerender.md) | Helps avoid patterns that often lead to excess or broken renders | 🐞⚡️ ||
| no-methods-in-models | restricts adding methods to model classes | usage ||
| no-computeds-in-models | restricts adding computed properties to model classes | usage ||
| no-getters-in-models | restricts adding getters/setters to model classes | usage ||
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-plugin-ember-data/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# eslint-plugin-warp-drive

## Rules

- [no-create-record-rerender](./no-create-record-rerender.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# eslint-plugin-warp-drive

## no-create-record-rerender

Helps avoid patterns that often lead to excess or broken renders.

## Don't use `createRecord` inside constructors, getters, or class properties

Calling `createRecord` in these places can cause unexpected re-renders and may blow up in EmberData 4.12+.

### Incorrect Code

```gjs
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { on } from '@ember/modifier';
class MyForm extends Component {
@service store;
// ERROR: Cannot call `store.createRecord` in a class property initializer.
// Calling `store.createRecord` inside constructors, getters, and class
// properties can cause issues with re-renders.
model = this.store.createRecord('user');
<template>
{{!-- Some Template !--}}
</template>
}
export default ParentComponent extends Component {
@tracked isShowingForm = false;
@action rerenderWithForm() {
this.isShowingForm = true;
}
<template>
{{#if this.isShowingForm}}
<MyForm />
{{/if}}
<button type="button" {{on "click" this.rerenderWithForm}}>Show the form</button>
</template>
}
```

### Correct Code

```gjs
// app/components/parent-component.gts
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { on } from '@ember/modifier';
class MyForm extends Component {
<template>
{{!-- Some Template !--}}
</template>
}
export default class ParentComponent extends Component {
@tracked isShowingForm = false;
rerenderForm = () => {
this.model = this.store.createRecord('user');
this.isShowingForm = true;
}
<template>
{{#if this.isShowingForm}}
<MyForm @model={{this.model}} />
{{/if}}
<button type="button" {{on "click" this.rerenderForm}}>Show the child component</button>
</template>
}
```
1 change: 1 addition & 0 deletions packages/eslint-plugin-ember-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"files": [
"src",
"docs",
"CHANGELOG.md",
"README.md"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
description: 'Disallow use of `store.createRecord` in getters, constructors, and class properties',
category: 'Possible Errors',
recommended: true,
url: 'https://ember-luna.com/docs/5-patterns/best-practices/general#dontusecreaterecordinsideconstructorsgettersorclassproperties',
url: 'https://github.com/emberjs/data/tree/main/packages/eslint-plugin-ember-data/docs/rules/no-create-record-rerender.md',
},
messages: {
[messageId]:
Expand Down

0 comments on commit 25eabbf

Please sign in to comment.