// components/counter-component.js
import Component from '@ember/component';
import { computed } from '@ember/object';
import { equal } from '@ember/object/computed';
export default Component.extend({
count: 0,
isMin: equal('count', 0),
isMax: computed(
'count', 'maxCount',
function() {
return this.count === this.maxCount;
}
),
actions: {
inc() {
this.set('count', this.count + 1);
},
dec() {
this.set('count', this.count - 1);
}
}
});
{{!-- application.hbs --}}
{{counter-component
maxCount=10
}}
{{!-- counter-component.hbs --}}
<h2> {{count}} </h2>
<h2> MAX: {{maxCount}} </h2>
<div>
<button disabled={{isMin}} {{action "dec"}}>
-
</button>
<button disabled={{isMax}} {{action "inc"}}>
+
</button>
</div>
// components/counter-component.js
import Component from '@ember/component';
export default Component.extend({
count: 0,
...
});
class Counter {
count = 0;
increment() {
this.count++;
}
decrement() {
this.count--;
}
}
import Component from '@ember/component';
class counter extends Component {
count = 0;
increment() {
this.count++;
}
decrement() {
this.count--;
}
}
class counter {
increment() {
...
}
}
import debounce from 'debounce';
class counter {
@debounce
increment() {
...
}
}
// services/user.js
import Service from '@ember/service';
import { action } from '@ember/object';
export default Service.extend({
@action
showCurrentUser() {
let { users, userId } = this;
let currentUser = users.findBy('id', userId);
...
}
});
{{!-- components/user-profile.hbs --}}
<button {{action this.userService.showCurrentUser}}>
Show User
</button>
{{!-- modifer --}}
<button {{action "inc"}}>
+
</button>
{{!-- helper --}}
<button onclick={{action "inc"}}>
+
</button>
<button {{on "click" this.inc}}>
+
</button>
<button {{on "click" this.inc}}>
+
</button>
<button {{on "mouseover" this.inc}}>
+
</button>
<button {{on "click" this.inc}}>
+
</button>
export default Component.extend({
...
inc() {
this.set('count', this.count + 1);
}
});
<button {{on "click" this.inc}}>
+
</button>
import { action } from '@ember/object';
export default Component.extend({
...
@action inc() {
this.set('count', this.count + 1);
}
});
<button {{on "click" (fn this.inc 10)}}>
+
</button>
// components/counter-component.js
import Component from '@ember/component';
import { computed, action } from '@ember/object';
import { equal } from '@ember/object/computed';
export default class Counter extends Component {
count = 0;
@equal('count', 0) isMin;
@computed('count', 'maxCount')
get isMax() {
return this.count === this.maxCount;
}
@action inc() {
this.set('count', this.count + 1);
}
@action dec() {
this.set('count', this.count - 1);
}
});
// components/counter-component.js
import Component from '@ember/component';
import { computed } from '@ember/object';
import { equal } from '@ember/object/computed';
export default Component.extend({
count: 0,
isMin: equal('count', 0),
isMax: computed(
'count', 'maxCount',
function() {
return this.count === this.maxCount;
}
),
actions: {
inc() {
this.set('count', this.count + 1);
},
dec() {
this.set('count', this.count - 1);
},
}
});
<CounterComponent
@maxCount={{10}}
class="text-red"
/>
{{counter-component
maxCount=10
class="text-red"
}}
{{!-- application.hbs --}}
<CounterComponent
@maxCount={{10}}
class="text-red"
/>
{{!-- application.hbs --}}
<CounterComponent
@maxCount={{10}}
class="text-red"
/>
{{!-- counter-component.hbs --}}
<div> {{@maxCount}} </div>
{{!-- counter-component.hbs --}}
<div> {{this.count}} </div>
<div> {{@maxCount}} </div>
// components/counter-component.js
import Component from '@ember/component';
export default class Counter extends Component {
count = 0;
...
});
{{!-- application.hbs --}}
<Counter
@maxCount={{10}}
class="text-red"
/>
{{!-- application.hbs --}}
<Input /> | <input />
{{!-- application.hbs --}}
<Counter
@maxCount={{10}}
title="Ember Counter"
role="counter"
...
/>
// components/counter.js
export default Component.extend({
attributeBindings: [
'title', 'role',
...
]
});
{{!-- counter-component.hbs --}}
<div ...attributes>
{{this.count}}
...
</div>
{{firstName}}
{{fullName}}
{{greet-user}}
{{capitalize-user}}
{{@firstName}}
{{this.fullName}}
<GreetUser />
{{capitalize-user}}
{{!-- application.hbs --}}
<Counter
@maxCount={{10}}
/>
{{!-- counter.hbs --}}
<h2> {{this.count}} </h2>
<h2> MAX: {{@maxCount}} </h2>
<div>
<button
disabled={{this.isMin}}
{{on "click" this.dec}}>
-
</button>
<button
disabled={{this.isMax}}
{{on "click" this.inc}}>
+
</button>
</div>
{{!-- counter-component.hbs --}}
<h2> {{count}} </h2>
<h2> MAX: {{maxCount}} </h2>
<div>
<button
disabled={{isMin}}
{{on "click" this.dec}}>
-
</button>
<button
disabled={{isMax}}
{{on "click" this.inc}}>
+
</button>
</div>
{{!-- application.hbs --}}
{{counter-component
maxCount=10
}}
// components/counter-component.js
import Component from '@ember/component';
import { tracked } from '@ember/object';
export default class Counter extends Component {
@tracked count = 0;
...
});
// components/counter-component.js
import Component from '@ember/component';
import { tracked } from '@ember/object';
export default class Counter extends Component {
@tracked count = 0;
@action inc() {
this.count = this.count + 1;
// or
// this.count++;
}
});
// components/counter-component.js
import Component from '@ember/component';
import { tracked, computed } from '@ember/object';
export default class Counter extends Component {
@tracked count = 0;
@computed('count', 'maxCount')
get isMax() {
return this.count === this.maxCount;
}
});
// components/counter.js
import Component from '@ember/component';
import { action } from '@ember/object';
import { equal } from '@ember/object/computed';
import { tracked } from '@glimmer/tracking';
export default class Counter extends Component {
@tracked count = 0;
@equal('count', 0) isMin;
get isMax() {
return this.count === this.maxCount;
}
@action inc() {
this.count++;
}
@action dec() {
this.count--;
}
});
// components/counter.js
import Component from '@ember/component';
import { computed, action } from '@ember/object';
import { equal } from '@ember/object/computed';
export default class Counter extends Component {
count = 0;
@equal('count', 0) isMin;
@computed('count', 'maxCount')
get isMax() {
return this.count === this.maxCount;
}
@action inc() {
this.set('count', this.count + 1);
}
@action dec() {
this.set('count', this.count - 1);
}
});
interface GlimmerComponent<T = object> {
args: T;
isDestroying: boolean;
isDestroyed: boolean;
constructor(owner: Opaque, args: T): void;
willDestroy(): void;
}
// components/counter.js
import Component from '@ember/component';
export default Component.extends({
classNames: [
'bg-teal-100', 'text-center'
],
classNameBindings: ['isMax:text-red-100'],
...
})
// components/counter.js
import Component from '@ember/component';
export default Component.extends({
classNames: [
'bg-teal-100', 'text-center'
],
classNameBindings: ['isMax:text-red-100'],
...
})
{{!-- counter.hbs --}}
<div class="bg-teal-100 text-center
{{if isMin 'text-grey-100'}}
{{if isMin 'text-red-100'}}"
>
...
</div>
// components/counter.js
import Component from '@glimmer/component';
export default class Counter extends Component {
get isMax() {
return this.count === this.args.maxCount;
}
...
});
// components/counter.js
import Component from '@glimmer/component';
export default class Counter extends Component {
updateMaxCount(maxCount) {
this.args.maxCount = maxCount;
}
});
// components/counter.js
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { equal } from '@ember/object/computed';
import { tracked } from '@glimmer/tracking';
export default class Counter extends Component {
@tracked count = 0;
@equal('count', 0) isMin;
get isMax() {
return this.count === this.args.maxCount;
}
@action inc() {
this.count++;
}
@action dec() {
this.count--;
}
});
// components/counter.js
import Component from '@ember/component';
import { action } from '@ember/object';
import { equal } from '@ember/object/computed';
import { tracked } from '@glimmer/tracking';
export default class Counter extends Component {
@tracked count = 0;
@equal('count', 0) isMin;
get isMax() {
return this.count === this.maxCount;
}
@action inc() {
this.count++;
}
@action dec() {
this.count--;
}
});
import Component from '@ember/component';
import { computed } from '@ember/object';
import { equal } from '@ember/object/computed';
export default Component.extend({
count: 0,
isMin: equal('count', 0),
isMax: computed('count', 'maxCount', function() {
return this.count === this.maxCount;
}
),
actions: {
inc() {
this.set('count', this.count + 1);
},
dec() {
this.set('count', this.count - 1);
},
}
});
{{counter maxCount=10 }}
<h2> {{count}} </h2>
<h2> MAX: {{maxCount}} </h2>
<div>
<button disabled={{isMin}} {{action "dec"}}>
-
</button>
<button disabled={{isMax}} {{action "inc"}}>
+
</button>
</div>
<Counter @maxCount={{10}}/>
<h2> {{this.count}} </h2>
<h2> MAX: {{@maxCount}} </h2>
<div>
<button disabled={{this.isMin}} {{on "click" this.dec}}>
-
</button>
<button disabled={{this.isMax}} {{on "click" this.inc}}>
+
</button>
</div>
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { equal } from '@ember/object/computed';
import { tracked } from '@glimmer/tracking';
export default class Counter extends Component {
@tracked count = 0;
@equal('count', 0) isMin;
get isMax() {
return this.count === this.args.maxCount;
}
@action inc() {
this.count++;
}
@action dec() {
this.count--;
}
});
yarn add global ember-cli
ember new -b @ember/octane-app-blueprint octane-pokedex