ember-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEmber.js Best Practices
Ember.js 最佳实践
Comprehensive performance optimization and accessibility guide for Ember.js applications. Contains 58 rules across 9 categories, prioritized by impact to guide automated refactoring and code generation.
这是一份针对Ember.js应用的全面性能优化与可访问性指南。包含9个类别共58条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Ember components or routes
- Implementing data fetching with WarpDrive
- Reviewing code for performance issues
- Refactoring existing Ember.js code
- Optimizing bundle size or load times
- Implementing accessibility features
参考本指南的场景:
- 编写新的Ember组件或路由
- 使用WarpDrive实现数据获取
- 评审代码以排查性能问题
- 重构现有Ember.js代码
- 优化包体积或加载时间
- 实现可访问性功能
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Route Loading and Data Fetching | CRITICAL | |
| 2 | Build and Bundle Optimization | CRITICAL | |
| 3 | Component and Reactivity | HIGH | |
| 4 | Accessibility Best Practices | HIGH | |
| 5 | Service and State Management | MEDIUM-HIGH | |
| 6 | Template Optimization | MEDIUM | |
| 7 | Performance Optimization | MEDIUM | |
| 8 | Testing Best Practices | MEDIUM | |
| 9 | Tooling and Configuration | MEDIUM | |
| 10 | Advanced Patterns | MEDIUM-HIGH | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 路由加载与数据获取 | 关键 | |
| 2 | 构建与包优化 | 关键 | |
| 3 | 组件与响应式 | 高 | |
| 4 | 可访问性最佳实践 | 高 | |
| 5 | 服务与状态管理 | 中高 | |
| 6 | 模板优化 | 中 | |
| 7 | 性能优化 | 中 | |
| 8 | 测试最佳实践 | 中 | |
| 9 | 工具与配置 | 中 | |
| 10 | 高级模式 | 中高 | |
Quick Reference
快速参考
1. Route Loading and Data Fetching (CRITICAL)
1. 路由加载与数据获取(关键)
- - Use RSVP.hash() for parallel data loading
route-parallel-model - - Implement loading substates for better UX
route-loading-substates - - Use route-based code splitting with Embroider
route-lazy-routes - - Use route templates with co-located syntax
route-templates - - Implement smart route model caching
route-model-caching
- - 使用RSVP.hash()实现并行数据加载
route-parallel-model - - 实现加载子状态以提升用户体验
route-loading-substates - - 结合Embroider实现基于路由的代码分割
route-lazy-routes - - 使用带有协同定位语法的路由模板
route-templates - - 实现智能路由模型缓存
route-model-caching
2. Build and Bundle Optimization (CRITICAL)
2. 构建与包优化(关键)
- - Import directly, avoid entire namespaces
bundle-direct-imports - - Enable Embroider static mode for tree-shaking
bundle-embroider-static - - Lazy load heavy dependencies
bundle-lazy-dependencies
- - 直接导入,避免导入整个命名空间
bundle-direct-imports - - 启用Embroider静态模式以实现摇树优化
bundle-embroider-static - - 懒加载大型依赖
bundle-lazy-dependencies
3. Component and Reactivity Optimization (HIGH)
3. 组件与响应式优化(高)
- - Use Glimmer components over classic components
component-use-glimmer - - Use @cached for expensive computations
component-cached-getters - - Only track properties that affect rendering
component-minimal-tracking - - Use tracked-built-ins for complex state
component-tracked-toolbox - - Use yield blocks and contextual components
component-composition-patterns - - Build reactive chains with dependent getters
component-reactive-chains - - Use class fields for component composition
component-class-fields - - Implement controlled form patterns
component-controlled-forms - - Use {{on}} modifier for event handling
component-on-modifier - - Validate component arguments
component-args-validation - - Prevent memory leaks in components
component-memory-leaks - - Use strict mode and template-only components
component-strict-mode - - Avoid unnecessary classes in component examples
component-avoid-classes-in-examples - - Avoid constructors in Glimmer components
component-avoid-constructors - - Avoid legacy lifecycle hooks
component-avoid-lifecycle-hooks - - Follow proper file naming conventions
component-file-conventions - - Use named exports only
exports-named-only
- - 使用Glimmer组件替代传统组件
component-use-glimmer - - 为耗时计算使用@cached装饰器
component-cached-getters - - 仅跟踪影响渲染的属性
component-minimal-tracking - - 使用tracked-built-ins处理复杂状态
component-tracked-toolbox - - 使用yield块与上下文组件
component-composition-patterns - - 基于依赖getter构建响应式链
component-reactive-chains - - 使用类字段实现组件组合
component-class-fields - - 实现受控表单模式
component-controlled-forms - - 使用{{on}}修饰符处理事件
component-on-modifier - - 验证组件参数
component-args-validation - - 防止组件内存泄漏
component-memory-leaks - - 使用严格模式与仅模板组件
component-strict-mode - - 组件示例中避免不必要的类
component-avoid-classes-in-examples - - Glimmer组件中避免使用构造函数
component-avoid-constructors - - 避免使用旧版生命周期钩子
component-avoid-lifecycle-hooks - - 遵循正确的文件命名规范
component-file-conventions - - 仅使用命名导出
exports-named-only
4. Accessibility Best Practices (HIGH)
4. 可访问性最佳实践(高)
- - Use ember-a11y-testing for automated checks
a11y-automated-testing - - Use semantic HTML and proper ARIA attributes
a11y-semantic-html - - Ensure full keyboard navigation support
a11y-keyboard-navigation - - Associate labels with inputs, announce errors
a11y-form-labels - - Announce route transitions to screen readers
a11y-route-announcements
- - 使用ember-a11y-testing进行自动化检查
a11y-automated-testing - - 使用语义化HTML与正确的ARIA属性
a11y-semantic-html - - 确保支持完整的键盘导航
a11y-keyboard-navigation - - 关联标签与输入框,提示错误信息
a11y-form-labels - - 向屏幕阅读器播报路由切换信息
a11y-route-announcements
5. Service and State Management (MEDIUM-HIGH)
5. 服务与状态管理(中高)
- - Cache API responses in services
service-cache-responses - - Use services for shared state
service-shared-state - - Optimize WarpDrive queries
service-ember-data-optimization - - Manage service owner and linkage patterns
service-owner-linkage - - Implement robust data requesting patterns
service-data-requesting
- - 在服务中缓存API响应
service-cache-responses - - 使用服务管理共享状态
service-shared-state - - 优化WarpDrive查询
service-ember-data-optimization - - 管理服务所有者与关联模式
service-owner-linkage - - 实现健壮的数据请求模式
service-data-requesting
6. Template Optimization (MEDIUM)
6. 模板优化(中)
- - Use {{#let}} to avoid recomputation
template-let-helper - - Use {{#each}} with @key for efficient list updates
template-each-key - - Move expensive work to cached getters
template-avoid-computation - - Import helpers directly in templates
template-helper-imports - - Optimize conditional rendering
template-conditional-rendering - - Use {{fn}} helper for partial application
template-fn-helper - - Use template-only components
template-only-component-functions - - Compose helpers for reusable logic
helper-composition - - Use built-in helpers effectively
helper-builtin-functions - - Write helpers as plain functions
helper-plain-functions
- - 使用{{#let}}避免重复计算
template-let-helper - - 为{{#each}}添加@key以实现高效列表更新
template-each-key - - 将耗时操作移至缓存getter中
template-avoid-computation - - 在模板中直接导入助手函数
template-helper-imports - - 优化条件渲染
template-conditional-rendering - - 使用{{fn}}助手函数实现部分应用
template-fn-helper - - 使用仅模板组件
template-only-component-functions - - 组合助手函数实现可复用逻辑
helper-composition - - 高效使用内置助手函数
helper-builtin-functions - - 将助手函数编写为普通函数
helper-plain-functions
7. Performance Optimization (MEDIUM)
7. 性能优化(中)
- - Use {{on}} modifier instead of event handler properties
performance-on-modifier-vs-handlers
- - 使用{{on}}修饰符替代事件处理属性
performance-on-modifier-vs-handlers
8. Testing Best Practices (MEDIUM)
8. 测试最佳实践(中)
- - Use modern testing patterns
testing-modern-patterns - - Use qunit-dom for better test assertions
testing-qunit-dom-assertions - - Use @ember/test-waiters for async testing
testing-test-waiters - - Use correct render patterns for components
testing-render-patterns - - Mock API requests with MSW
testing-msw-setup - - Use Testing Library patterns
testing-library-dom-abstraction
- - 使用现代测试模式
testing-modern-patterns - - 使用qunit-dom实现更优的测试断言
testing-qunit-dom-assertions - - 使用@ember/test-waiters处理异步测试
testing-test-waiters - - 为组件使用正确的渲染模式
testing-render-patterns - - 使用MSW模拟API请求
testing-msw-setup - - 使用Testing Library模式
testing-library-dom-abstraction
9. Tooling and Configuration (MEDIUM)
9. 工具与配置(中)
- - VS Code extensions and MCP server setup
vscode-setup-recommended
- - VS Code扩展与MCP服务器设置
vscode-setup-recommended
10. Advanced Patterns (MEDIUM-HIGH)
10. 高级模式(中高)
- - Use modifiers for DOM side effects
advanced-modifiers - - Extract reusable logic into helpers
advanced-helpers - - Use reactive collections from @ember/reactive/collections
advanced-tracked-built-ins - - Use ember-concurrency for task management
advanced-concurrency - - Data loading patterns with ember-concurrency
advanced-data-loading-with-ember-concurrency
- - 使用修饰符处理DOM副作用
advanced-modifiers - - 将可复用逻辑提取到助手函数中
advanced-helpers - - 使用@ember/reactive/collections中的响应式集合
advanced-tracked-built-ins - - 使用ember-concurrency进行任务管理
advanced-concurrency - - 结合ember-concurrency实现数据加载模式
advanced-data-loading-with-ember-concurrency
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/route-parallel-model.md
rules/bundle-embroider-static.md
rules/a11y-automated-testing.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读单个规则文件以获取详细说明和代码示例:
rules/route-parallel-model.md
rules/bundle-embroider-static.md
rules/a11y-automated-testing.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及解释
- 正确代码示例及解释
- 额外背景信息与参考资料
Accessibility with OSS Tools
借助开源工具实现可访问性
Ember has excellent accessibility support through community addons:
- ember-a11y-testing - Automated accessibility testing in your test suite
- ember-a11y - Route announcements and focus management
- ember-focus-trap - Focus trapping for modals and dialogs
- ember-page-title - Accessible page title management
- Platform-native validation - Use browser's Constraint Validation API for accessible form validation
These tools, combined with native web platform features, provide comprehensive a11y support with minimal configuration.
Ember通过社区插件提供了出色的可访问性支持:
- ember-a11y-testing - 在测试套件中实现自动化可访问性测试
- ember-a11y - 路由播报与焦点管理
- ember-focus-trap - 为模态框与对话框实现焦点捕获
- ember-page-title - 可访问的页面标题管理
- 平台原生验证 - 使用浏览器的Constraint Validation API实现可访问的表单验证
这些工具结合原生Web平台特性,只需极少配置即可提供全面的可访问性支持。
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有展开规则的完整指南,请查阅:
AGENTS.md