Skip to content
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

Ember "Context" Example #160

Merged
merged 2 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions content/4-component-composition/5-context/ember/app.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<UserProfile />
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
<h2>My Profile</h2>
<p>Username: {{this.user.username}}</p>
<p>Email: {{this.user.email}}</p>
<button {{on "click" (fn this.userService.updateUserName "Jane")}}>
Update username to Jane
</button>
</div>
10 changes: 10 additions & 0 deletions content/4-component-composition/5-context/ember/user-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Component from '@glimmer/component';
import { service } from '@ember/service';

export default class UserProfileComponent extends Component {
@service userService;

get user() {
return this.userService.user;
}
}
22 changes: 22 additions & 0 deletions content/4-component-composition/5-context/ember/user-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class UserServiceService extends Service {
@tracked id = 1;
@tracked username = 'unicorn42';
@tracked email = '[email protected]';

get user() {
return {
id: this.id,
username: this.username,
email: this.email,
};
}

@action
updateUserName(newUsername) {
this.username = newUsername;
}
}