Loading...
Loading...
Compare original and translation side by side
// Example: Check for Ember component patterns before creating a component
// Use the context7 MCP to search for relevant documentation
// Query examples:
// - "ember component patterns"
// - "ember routing conventions"
// - "ember data models"
// - "ember testing standards"
// - "glimmer component lifecycle"
// Apply the documentation from context7 to your implementation// Example: Check for Ember component patterns before creating a component
// Use the context7 MCP to search for relevant documentation
// Query examples:
// - "ember component patterns"
// - "ember routing conventions"
// - "ember data models"
// - "ember testing standards"
// - "glimmer component lifecycle"
// Apply the documentation from context7 to your implementation- "ember component patterns"
- "ember component architecture"
- "glimmer component conventions"
- "[specific component type]" (e.g., "button component", "form component")
- "component props" or "component arguments"
- "component lifecycle"
- "component testing"- "ember routing patterns"
- "ember route conventions"
- "route data loading"
- "route guards" or "route authentication"
- "nested routes"
- "query parameters"- "ember data models"
- "ember data adapters"
- "ember data serializers"
- "api integration"
- "data relationships"
- "data layer patterns"- "ember services"
- "service patterns"
- "shared state management"
- "[specific service]" (e.g., "authentication service", "api service")- "ember testing"
- "component testing"
- "integration tests"
- "acceptance tests"
- "test patterns"
- "test data" or "fixtures"- "ember styling"
- "css conventions"
- "component styles"
- "tailwind" or "[css framework]"- Search for the specific feature: "user profile", "login form", etc.
- Search for the pattern you're implementing: "form validation", "dropdown menu"
- Search for utilities you might need: "validation utilities", "date helpers"- "ember component patterns"
- "ember component architecture"
- "glimmer component conventions"
- "[specific component type]" (e.g., "button component", "form component")
- "component props" or "component arguments"
- "component lifecycle"
- "component testing"- "ember routing patterns"
- "ember route conventions"
- "route data loading"
- "route guards" or "route authentication"
- "nested routes"
- "query parameters"- "ember data models"
- "ember data adapters"
- "ember data serializers"
- "api integration"
- "data relationships"
- "data layer patterns"- "ember services"
- "service patterns"
- "shared state management"
- "[specific service]" (e.g., "authentication service", "api service")- "ember testing"
- "component testing"
- "integration tests"
- "acceptance tests"
- "test patterns"
- "test data" or "fixtures"- "ember styling"
- "css conventions"
- "component styles"
- "tailwind" or "[css framework]"- 搜索特定功能:"user profile", "login form"等
- 搜索你要实现的模式:"form validation", "dropdown menu"
- 搜索你可能需要的工具:"validation utilities", "date helpers"// app/components/user-profile.js
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class UserProfileComponent extends Component {
@service currentUser;
@service router;
@tracked isEditing = false;
@tracked formData = null;
constructor(owner, args) {
super(owner, args);
// Use constructor for one-time setup
this.formData = { ...this.args.user };
}
@action
toggleEdit() {
this.isEditing = !this.isEditing;
if (this.isEditing) {
this.formData = { ...this.args.user };
}
}
@action
async saveUser(event) {
event.preventDefault();
try {
await this.args.onSave(this.formData);
this.isEditing = false;
} catch (error) {
// Handle error
console.error('Save failed:', error);
}
}
@action
updateField(field, event) {
this.formData = {
...this.formData,
[field]: event.target.value
};
}
}
<div class="user-profile">
<form >
<label>
Name:
<input
type="text"
value=
/>
</label>
<label>
Email:
<input
type="email"
value=
/>
</label>
<button type="submit">Save</button>
<button type="button" >Cancel</button>
</form>
<div class="profile-display">
<h2></h2>
<p></p>
<button >Edit Profile</button>
</div>
</div>// app/components/user-profile.js
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class UserProfileComponent extends Component {
@service currentUser;
@service router;
@tracked isEditing = false;
@tracked formData = null;
constructor(owner, args) {
super(owner, args);
// Use constructor for one-time setup
this.formData = { ...this.args.user };
}
@action
toggleEdit() {
this.isEditing = !this.isEditing;
if (this.isEditing) {
this.formData = { ...this.args.user };
}
}
@action
async saveUser(event) {
event.preventDefault();
try {
await this.args.onSave(this.formData);
this.isEditing = false;
} catch (error) {
// Handle error
console.error('Save failed:', error);
}
}
@action
updateField(field, event) {
this.formData = {
...this.formData,
[field]: event.target.value
};
}
}
<div class="user-profile">
<form >
<label>
Name:
<input
type="text"
value=
/>
</label>
<label>
Email:
<input
type="email"
value=
/>
</label>
<button type="submit">Save</button>
<button type="button" >Cancel</button>
</form>
<div class="profile-display">
<h2></h2>
<p></p>
<button >Edit Profile</button>
</div>
</div>import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { cached } from '@glimmer/tracking';
export default class DataGridComponent extends Component {
@tracked sortColumn = 'name';
@tracked sortDirection = 'asc';
@tracked filterText = '';
// Use @cached for expensive computations that depend on tracked properties
@cached
get filteredData() {
const { filterText } = this;
if (!filterText) return this.args.data;
const lower = filterText.toLowerCase();
return this.args.data.filter(item =>
item.name.toLowerCase().includes(lower) ||
item.email.toLowerCase().includes(lower)
);
}
@cached
get sortedData() {
const data = [...this.filteredData];
const { sortColumn, sortDirection } = this;
return data.sort((a, b) => {
const aVal = a[sortColumn];
const bVal = b[sortColumn];
const result = aVal < bVal ? -1 : aVal > bVal ? 1 : 0;
return sortDirection === 'asc' ? result : -result;
});
}
}import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { cached } from '@glimmer/tracking';
export default class DataGridComponent extends Component {
@tracked sortColumn = 'name';
@tracked sortDirection = 'asc';
@tracked filterText = '';
// Use @cached for expensive computations that depend on tracked properties
@cached
get filteredData() {
const { filterText } = this;
if (!filterText) return this.args.data;
const lower = filterText.toLowerCase();
return this.args.data.filter(item =>
item.name.toLowerCase().includes(lower) ||
item.email.toLowerCase().includes(lower)
);
}
@cached
get sortedData() {
const data = [...this.filteredData];
const { sortColumn, sortDirection } = this;
return data.sort((a, b) => {
const aVal = a[sortColumn];
const bVal = b[sortColumn];
const result = aVal < bVal ? -1 : aVal > bVal ? 1 : 0;
return sortDirection === 'asc' ? result : -result;
});
}
}// app/services/notification.js
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class NotificationService extends Service {
@tracked notifications = [];
@action
add(message, type = 'info', duration = 5000) {
const id = Date.now() + Math.random();
const notification = { id, message, type };
this.notifications = [...this.notifications, notification];
if (duration > 0) {
setTimeout(() => this.remove(id), duration);
}
return id;
}
@action
remove(id) {
this.notifications = this.notifications.filter(n => n.id !== id);
}
@action
success(message, duration) {
return this.add(message, 'success', duration);
}
@action
error(message, duration = 10000) {
return this.add(message, 'error', duration);
}
@action
clear() {
this.notifications = [];
}
}// app/services/notification.js
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class NotificationService extends Service {
@tracked notifications = [];
@action
add(message, type = 'info', duration = 5000) {
const id = Date.now() + Math.random();
const notification = { id, message, type };
this.notifications = [...this.notifications, notification];
if (duration > 0) {
setTimeout(() => this.remove(id), duration);
}
return id;
}
@action
remove(id) {
this.notifications = this.notifications.filter(n => n.id !== id);
}
@action
success(message, duration) {
return this.add(message, 'success', duration);
}
@action
error(message, duration = 10000) {
return this.add(message, 'error', duration);
}
@action
clear() {
this.notifications = [];
}
}// app/modifiers/click-outside.js
import { modifier } from 'ember-modifier';
export default modifier((element, [callback]) => {
function handleClick(event) {
if (!element.contains(event.target)) {
callback(event);
}
}
document.addEventListener('click', handleClick, true);
return () => {
document.removeEventListener('click', handleClick, true);
};
});
<div class="dropdown">
</div>// app/modifiers/click-outside.js
import { modifier } from 'ember-modifier';
export default modifier((element, [callback]) => {
function handleClick(event) {
if (!element.contains(event.target)) {
callback(event);
}
}
document.addEventListener('click', handleClick, true);
return () => {
document.removeEventListener('click', handleClick, true);
};
});
<div class="dropdown">
</div>// app/router.js
import EmberRouter from '@ember/routing/router';
import config from 'my-app/config/environment';
export default class Router extends EmberRouter {
location = config.locationType;
rootURL = config.rootURL;
}
Router.map(function () {
this.route('dashboard', { path: '/' });
this.route('users', function () {
this.route('index', { path: '/' });
this.route('new');
this.route('user', { path: '/:user_id' }, function () {
this.route('edit');
this.route('settings');
});
});
this.route('not-found', { path: '/*path' });
});// app/router.js
import EmberRouter from '@ember/routing/router';
import config from 'my-app/config/environment';
export default class Router extends EmberRouter {
location = config.locationType;
rootURL = config.rootURL;
}
Router.map(function () {
this.route('dashboard', { path: '/' });
this.route('users', function () {
this.route('index', { path: '/' });
this.route('new');
this.route('user', { path: '/:user_id' }, function () {
this.route('edit');
this.route('settings');
});
});
this.route('not-found', { path: '/*path' });
});// app/routes/users/user.js
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class UsersUserRoute extends Route {
@service store;
@service router;
async model(params) {
try {
return await this.store.findRecord('user', params.user_id, {
include: 'profile,settings'
});
} catch (error) {
if (error.errors?.[0]?.status === '404') {
this.router.transitionTo('not-found');
}
throw error;
}
}
// Redirect if user doesn't have permission
afterModel(model) {
if (!this.currentUser.canViewUser(model)) {
this.router.transitionTo('dashboard');
}
}
// Reset controller state on exit
resetController(controller, isExiting) {
if (isExiting) {
controller.setProperties({
queryParams: {},
isEditing: false
});
}
}
}// app/routes/users/user.js
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class UsersUserRoute extends Route {
@service store;
@service router;
async model(params) {
try {
return await this.store.findRecord('user', params.user_id, {
include: 'profile,settings'
});
} catch (error) {
if (error.errors?.[0]?.status === '404') {
this.router.transitionTo('not-found');
}
throw error;
}
}
// Redirect if user doesn't have permission
afterModel(model) {
if (!this.currentUser.canViewUser(model)) {
this.router.transitionTo('dashboard');
}
}
// Reset controller state on exit
resetController(controller, isExiting) {
if (isExiting) {
controller.setProperties({
queryParams: {},
isEditing: false
});
}
}
}// app/routes/users/user/loading.js
import Route from '@ember/routing/route';
export default class UsersUserLoadingRoute extends Route {}
<div class="loading-spinner">
<p>Loading user...</p>
</div>// app/routes/users/user/loading.js
import Route from '@ember/routing/route';
export default class UsersUserLoadingRoute extends Route {}
<div class="loading-spinner">
<p>Loading user...</p>
</div>// app/models/user.js
import Model, { attr, hasMany, belongsTo } from '@ember-data/model';
export default class UserModel extends Model {
@attr('string') name;
@attr('string') email;
@attr('date') createdAt;
@attr('boolean', { defaultValue: true }) isActive;
@attr('number') loginCount;
@belongsTo('profile', { async: true, inverse: 'user' }) profile;
@hasMany('post', { async: true, inverse: 'author' }) posts;
// Computed properties still work but use native getters
get displayName() {
return this.name || this.email?.split('@')[0] || 'Anonymous';
}
get isNewUser() {
const daysSinceCreation = (Date.now() - this.createdAt) / (1000 * 60 * 60 * 24);
return daysSinceCreation < 7;
}
}// app/models/user.js
import Model, { attr, hasMany, belongsTo } from '@ember-data/model';
export default class UserModel extends Model {
@attr('string') name;
@attr('string') email;
@attr('date') createdAt;
@attr('boolean', { defaultValue: true }) isActive;
@attr('number') loginCount;
@belongsTo('profile', { async: true, inverse: 'user' }) profile;
@hasMany('post', { async: true, inverse: 'author' }) posts;
// Computed properties still work but use native getters
get displayName() {
return this.name || this.email?.split('@')[0] || 'Anonymous';
}
get isNewUser() {
const daysSinceCreation = (Date.now() - this.createdAt) / (1000 * 60 * 60 * 24);
return daysSinceCreation < 7;
}
}// app/adapters/application.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import { inject as service } from '@ember/service';
export default class ApplicationAdapter extends JSONAPIAdapter {
@service session;
host = 'https://api.example.com';
namespace = 'v1';
get headers() {
const headers = {};
if (this.session.isAuthenticated) {
headers['Authorization'] = `Bearer ${this.session.data.authenticated.token}`;
}
return headers;
}
handleResponse(status, headers, payload, requestData) {
if (status === 401) {
this.session.invalidate();
}
return super.handleResponse(status, headers, payload, requestData);
}
}// app/adapters/application.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import { inject as service } from '@ember/service';
export default class ApplicationAdapter extends JSONAPIAdapter {
@service session;
host = 'https://api.example.com';
namespace = 'v1';
get headers() {
const headers = {};
if (this.session.isAuthenticated) {
headers['Authorization'] = `Bearer ${this.session.data.authenticated.token}`;
}
return headers;
}
handleResponse(status, headers, payload, requestData) {
if (status === 401) {
this.session.invalidate();
}
return super.handleResponse(status, headers, payload, requestData);
}
}// app/serializers/application.js
import JSONAPISerializer from '@ember-data/serializer/json-api';
export default class ApplicationSerializer extends JSONAPISerializer {
// Normalize date strings to Date objects
normalizeDateFields(hash) {
const dateFields = ['createdAt', 'updatedAt', 'publishedAt'];
dateFields.forEach(field => {
if (hash[field]) {
hash[field] = new Date(hash[field]);
}
});
return hash;
}
normalize(modelClass, resourceHash) {
this.normalizeDateFields(resourceHash.attributes || {});
return super.normalize(modelClass, resourceHash);
}
}// app/serializers/application.js
import JSONAPISerializer from '@ember-data/serializer/json-api';
export default class ApplicationSerializer extends JSONAPISerializer {
// Normalize date strings to Date objects
normalizeDateFields(hash) {
const dateFields = ['createdAt', 'updatedAt', 'publishedAt'];
dateFields.forEach(field => {
if (hash[field]) {
hash[field] = new Date(hash[field]);
}
});
return hash;
}
normalize(modelClass, resourceHash) {
this.normalizeDateFields(resourceHash.attributes || {});
return super.normalize(modelClass, resourceHash);
}
}// tests/integration/components/user-profile-test.js
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, click, fillIn } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
module('Integration | Component | user-profile', function (hooks) {
setupRenderingTest(hooks);
test('it displays user information', async function (assert) {
this.set('user', {
name: 'Jane Doe',
email: 'jane@example.com'
});
await render(hbs`<UserProfile @user={{this.user}} />`);
assert.dom('h2').hasText('Jane Doe');
assert.dom('p').hasText('jane@example.com');
});
test('it allows editing when canEdit is true', async function (assert) {
this.owner.lookup('service:current-user').canEdit = true;
this.set('user', {
name: 'Jane Doe',
email: 'jane@example.com'
});
this.set('onSave', () => {});
await render(hbs`
<UserProfile @user={{this.user}} @onSave={{this.onSave}} />
`);
await click('button:contains("Edit Profile")');
assert.dom('form').exists();
assert.dom('input[type="text"]').hasValue('Jane Doe');
await fillIn('input[type="text"]', 'Jane Smith');
await click('button[type="submit"]');
assert.dom('form').doesNotExist();
});
});// tests/integration/components/user-profile-test.js
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, click, fillIn } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
module('Integration | Component | user-profile', function (hooks) {
setupRenderingTest(hooks);
test('it displays user information', async function (assert) {
this.set('user', {
name: 'Jane Doe',
email: 'jane@example.com'
});
await render(hbs`<UserProfile @user={{this.user}} />`);
assert.dom('h2').hasText('Jane Doe');
assert.dom('p').hasText('jane@example.com');
});
test('it allows editing when canEdit is true', async function (assert) {
this.owner.lookup('service:current-user').canEdit = true;
this.set('user', {
name: 'Jane Doe',
email: 'jane@example.com'
});
this.set('onSave', () => {});
await render(hbs`
<UserProfile @user={{this.user}} @onSave={{this.onSave}} />
`);
await click('button:contains("Edit Profile")');
assert.dom('form').exists();
assert.dom('input[type="text"]').hasValue('Jane Doe');
await fillIn('input[type="text"]', 'Jane Smith');
await click('button[type="submit"]');
assert.dom('form').doesNotExist();
});
});// tests/acceptance/user-flow-test.js
import { module, test } from 'qunit';
import { visit, currentURL, click, fillIn } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
module('Acceptance | user flow', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
test('visiting /users and creating a new user', async function (assert) {
await visit('/users');
assert.strictEqual(currentURL(), '/users');
assert.dom('h1').hasText('Users');
await click('a:contains("New User")');
assert.strictEqual(currentURL(), '/users/new');
await fillIn('[data-test-name-input]', 'John Doe');
await fillIn('[data-test-email-input]', 'john@example.com');
await click('[data-test-submit]');
assert.strictEqual(currentURL(), '/users/1');
assert.dom('[data-test-user-name]').hasText('John Doe');
});
});// tests/acceptance/user-flow-test.js
import { module, test } from 'qunit';
import { visit, currentURL, click, fillIn } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
module('Acceptance | user flow', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
test('visiting /users and creating a new user', async function (assert) {
await visit('/users');
assert.strictEqual(currentURL(), '/users');
assert.dom('h1').hasText('Users');
await click('a:contains("New User")');
assert.strictEqual(currentURL(), '/users/new');
await fillIn('[data-test-name-input]', 'John Doe');
await fillIn('[data-test-email-input]', 'john@example.com');
await click('[data-test-submit]');
assert.strictEqual(currentURL(), '/users/1');
assert.dom('[data-test-user-name]').hasText('John Doe');
});
});| Problem | Solution |
|---|---|
| Unnecessary component re-renders | Use |
| Large lists | Use |
| Slow Ember Data queries | Optimize includes, use custom serializers |
| Bundle size | Use route-based code splitting, lazy engines |
| Memory leaks | Properly clean up in willDestroy, cancel timers |
| 问题 | 解决方案 |
|---|---|
| 不必要的组件重渲染 | 对昂贵的getter使用 |
| 大型列表 | 使用 |
| 缓慢的Ember Data查询 | 优化includes,使用自定义序列化器 |
| 包体积过大 | 使用基于路由的代码分割、惰性引擎 |
| 内存泄漏 | 在willDestroy中正确清理,取消定时器 |
// ❌ Mutating tracked properties directly
this.items.push(newItem); // Won't trigger reactivity
// ✅ Replace the entire array
this.items = [...this.items, newItem];
// ❌ Creating new functions in templates
{{on "click" (fn this.handleClick item)}}
// ✅ Use actions or stable references
@action handleItemClick(item) { /* ... */ }
{{on "click" (fn this.handleItemClick item)}}
// ❌ Not using @cached for expensive computations
get expensiveComputation() {
return this.data.filter(/* complex logic */);
}
// ✅ Use @cached
@cached
get expensiveComputation() {
return this.data.filter(/* complex logic */);
}// ❌ 直接修改tracked属性
this.items.push(newItem); // 不会触发响应式更新
// ✅ 替换整个数组
this.items = [...this.items, newItem];
// ❌ 在模板中创建新函数
{{on "click" (fn this.handleClick item)}}
// ✅ 使用actions或稳定引用
@action handleItemClick(item) { /* ... */ }
{{on "click" (fn this.handleItemClick item)}}
// ❌ 不对昂贵的计算使用@cached
get expensiveComputation() {
return this.data.filter(/* complex logic */);
}
// ✅ 使用@cached
@cached
get expensiveComputation() {
return this.data.filter(/* complex logic */);
}app/
├── components/ # Glimmer components
│ └── user-profile/
│ ├── component.js
│ ├── index.hbs
│ └── styles.css
├── controllers/ # Controllers (use sparingly in Octane)
├── helpers/ # Template helpers
├── modifiers/ # Custom modifiers
├── models/ # Ember Data models
├── routes/ # Route classes
├── services/ # Services
├── templates/ # Route templates
├── adapters/ # Ember Data adapters
├── serializers/ # Ember Data serializers
├── styles/ # Global styles
└── app.js
tests/
├── integration/ # Component tests
├── unit/ # Unit tests (models, services, etc.)
└── acceptance/ # Full application testsapp/
├── components/ # Glimmer组件
│ └── user-profile/
│ ├── component.js
│ ├── index.hbs
│ └── styles.css
├── controllers/ # 控制器(Octane中尽量少用)
├── helpers/ # 模板助手
├── modifiers/ # 自定义修饰器
├── models/ # Ember Data模型
├── routes/ # 路由类
├── services/ # 服务
├── templates/ # 路由模板
├── adapters/ # Ember Data适配器
├── serializers/ # Ember Data序列化器
├── styles/ # 全局样式
└── app.js
tests/
├── integration/ # 组件测试
├── unit/ # 单元测试(模型、服务等)
└── acceptance/ # 全应用测试| Category | Tool | Notes |
|---|---|---|
| CLI | ember-cli | Official tooling |
| Testing | QUnit + ember-qunit | Built-in, well integrated |
| Linting | ESLint + ember-template-lint | Catch template issues |
| Formatting | Prettier | Use with ember-template-lint |
| Mocking | ember-cli-mirage | API mocking for tests |
| State management | Services + tracked | Built-in, no extra deps |
| HTTP | fetch or ember-fetch | Native or polyfilled |
| 类别 | 工具 | 说明 |
|---|---|---|
| CLI | ember-cli | 官方工具 |
| 测试 | QUnit + ember-qunit | 内置,集成良好 |
| 代码检查 | ESLint + ember-template-lint | 捕获模板问题 |
| 格式化 | Prettier | 与ember-template-lint配合使用 |
| 模拟 | ember-cli-mirage | 测试用API模拟 |
| 状态管理 | Services + tracked | 内置,无需额外依赖 |
| HTTP | fetch或ember-fetch | 原生或polyfill |
// app/components/registration-form.js
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class RegistrationFormComponent extends Component {
@service notification;
@tracked email = '';
@tracked password = '';
@tracked confirmPassword = '';
@tracked errors = {};
@tracked isSubmitting = false;
get isValid() {
return (
this.email &&
this.password.length >= 8 &&
this.password === this.confirmPassword &&
Object.keys(this.errors).length === 0
);
}
@action
updateEmail(event) {
this.email = event.target.value;
this.validateEmail();
}
@action
updatePassword(event) {
this.password = event.target.value;
this.validatePassword();
}
@action
updateConfirmPassword(event) {
this.confirmPassword = event.target.value;
this.validateConfirmPassword();
}
validateEmail() {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!this.email) {
this.errors = { ...this.errors, email: 'Email is required' };
} else if (!emailRegex.test(this.email)) {
this.errors = { ...this.errors, email: 'Invalid email format' };
} else {
const { email, ...rest } = this.errors;
this.errors = rest;
}
}
validatePassword() {
if (this.password.length < 8) {
this.errors = { ...this.errors, password: 'Password must be at least 8 characters' };
} else {
const { password, ...rest } = this.errors;
this.errors = rest;
}
// Re-validate confirm password if it's filled
if (this.confirmPassword) {
this.validateConfirmPassword();
}
}
validateConfirmPassword() {
if (this.password !== this.confirmPassword) {
this.errors = { ...this.errors, confirmPassword: 'Passwords do not match' };
} else {
const { confirmPassword, ...rest } = this.errors;
this.errors = rest;
}
}
@action
async submit(event) {
event.preventDefault();
if (!this.isValid) return;
this.isSubmitting = true;
try {
await this.args.onSubmit({
email: this.email,
password: this.password
});
this.notification.success('Registration successful!');
} catch (error) {
this.notification.error(error.message || 'Registration failed');
} finally {
this.isSubmitting = false;
}
}
}// app/components/registration-form.js
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class RegistrationFormComponent extends Component {
@service notification;
@tracked email = '';
@tracked password = '';
@tracked confirmPassword = '';
@tracked errors = {};
@tracked isSubmitting = false;
get isValid() {
return (
this.email &&
this.password.length >= 8 &&
this.password === this.confirmPassword &&
Object.keys(this.errors).length === 0
);
}
@action
updateEmail(event) {
this.email = event.target.value;
this.validateEmail();
}
@action
updatePassword(event) {
this.password = event.target.value;
this.validatePassword();
}
@action
updateConfirmPassword(event) {
this.confirmPassword = event.target.value;
this.validateConfirmPassword();
}
validateEmail() {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!this.email) {
this.errors = { ...this.errors, email: '邮箱为必填项' };
} else if (!emailRegex.test(this.email)) {
this.errors = { ...this.errors, email: '邮箱格式无效' };
} else {
const { email, ...rest } = this.errors;
this.errors = rest;
}
}
validatePassword() {
if (this.password.length < 8) {
this.errors = { ...this.errors, password: '密码长度至少为8位' };
} else {
const { password, ...rest } = this.errors;
this.errors = rest;
}
// 如果确认密码已填写,重新验证
if (this.confirmPassword) {
this.validateConfirmPassword();
}
}
validateConfirmPassword() {
if (this.password !== this.confirmPassword) {
this.errors = { ...this.errors, confirmPassword: '密码不匹配' };
} else {
const { confirmPassword, ...rest } = this.errors;
this.errors = rest;
}
}
@action
async submit(event) {
event.preventDefault();
if (!this.isValid) return;
this.isSubmitting = true;
try {
await this.args.onSubmit({
email: this.email,
password: this.password
});
this.notification.success('注册成功!');
} catch (error) {
this.notification.error(error.message || '注册失败');
} finally {
this.isSubmitting = false;
}
}
}// app/modifiers/infinite-scroll.js
import { modifier } from 'ember-modifier';
export default modifier((element, [callback], { threshold = 200 }) => {
let isLoading = false;
function handleScroll() {
if (isLoading) return;
const { scrollTop, scrollHeight, clientHeight } = element;
const distanceFromBottom = scrollHeight - (scrollTop + clientHeight);
if (distanceFromBottom < threshold) {
isLoading = true;
callback().finally(() => {
isLoading = false;
});
}
}
element.addEventListener('scroll', handleScroll, { passive: true });
return () => {
element.removeEventListener('scroll', handleScroll);
};
});// app/modifiers/infinite-scroll.js
import { modifier } from 'ember-modifier';
export default modifier((element, [callback], { threshold = 200 }) => {
let isLoading = false;
function handleScroll() {
if (isLoading) return;
const { scrollTop, scrollHeight, clientHeight } = element;
const distanceFromBottom = scrollHeight - (scrollTop + clientHeight);
if (distanceFromBottom < threshold) {
isLoading = true;
callback().finally(() => {
isLoading = false;
});
}
}
element.addEventListener('scroll', handleScroll, { passive: true });
return () => {
element.removeEventListener('scroll', handleScroll);
};
});ember install ember-cli-typescript// app/components/user-profile.ts
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import type { TOC } from '@ember/component/template-only';
interface UserProfileArgs {
user: {
name: string;
email: string;
avatarUrl?: string;
};
onSave: (data: UserData) => Promise<void>;
canEdit?: boolean;
}
interface UserData {
name: string;
email: string;
}
export default class UserProfileComponent extends Component<UserProfileArgs> {
@tracked isEditing = false;
@tracked formData: UserData | null = null;
@action
async saveUser(event: SubmitEvent): Promise<void> {
event.preventDefault();
if (!this.formData) return;
await this.args.onSave(this.formData);
this.isEditing = false;
}
}
// Template-only component signature
export interface GreetingSignature {
Element: HTMLDivElement;
Args: {
name: string;
};
}
const Greeting: TOC<GreetingSignature> = <template>
<div ...attributes>Hello {{@name}}!</div>
</template>;
export default Greeting;ember install ember-cli-typescript// app/components/user-profile.ts
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import type { TOC } from '@ember/component/template-only';
interface UserProfileArgs {
user: {
name: string;
email: string;
avatarUrl?: string;
};
onSave: (data: UserData) => Promise<void>;
canEdit?: boolean;
}
interface UserData {
name: string;
email: string;
}
export default class UserProfileComponent extends Component<UserProfileArgs> {
@tracked isEditing = false;
@tracked formData: UserData | null = null;
@action
async saveUser(event: SubmitEvent): Promise<void> {
event.preventDefault();
if (!this.formData) return;
await this.args.onSave(this.formData);
this.isEditing = false;
}
}
// Template-only component signature
export interface GreetingSignature {
Element: HTMLDivElement;
Args: {
name: string;
};
}
const Greeting: TOC<GreetingSignature> = <template>
<div ...attributes>Hello {{@name}}!</div>
</template>;
export default Greeting;