Ember.js Evented on() Method
Last Updated :
24 Apr, 2025
Ember.js is an open-source JavaScript web framework that follows the Model-View-Controller (MVC) pattern. It provides conventions and best practices for organizing code and building user interfaces. Ember.js is used to build scalable, single-page web applications.
The on() method can register a callback function to be called when a specific event takes place. The Ember event system contains a function called on() which is helpful in many cases such as clicks.
Syntax :
object.on(eventName, target, method);
Parameters: This method accepts three parameters that are described below:
- eventName: It is a string that specifies the name of the event you want to listen for.
- Target: It is the object which will be bound to the callback function.
- Method: It is the name of the callback function.
Return Value: This method returns this.
Steps to Install and Run Ember.js:
Step 1: To run the following examples, you will need to have an ember project with you. To create one, you will need to install ember-cli first. Write the below code in the terminal:
npm install ember-cli
Step 2: Now you can create the project by typing in the following piece of code:
ember new <project-name> --lang en
Step 3 : Navigate into the newly created directory :
cd <project-name>
Step 4 : Generate a new component and template for the click counter:
ember g component <project-name>
This will create a new route file at app/components/<project-name>
Example 1: Add a new template in the file at app/templates/components/<project-name>.hbs. Open the <project-name>.hbs file and add a button element with an {{on}} helper that will trigger a click event.
@app/templates/components/<project-name>.hbs
JavaScript
<p>Click Count = {{this.total}}</p>
<button type="button" {{on "click" (fn this.change 1)}}>
Click
</button>
@app/components/<project-name>.js
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class CounterComponent extends Component {
@tracked count = 0;
get total() {
return this.count * this.args.multiple;
}
@action change(amount) {
this.count = this.count + amount;
}
};
Output:
basic click counting application
Explanation: In this example, we define a component with a clickCount property that represents the number of times the component has been clicked. So whenever we click on the component, the click button will be increment by 1 every time. We use the on() method in this code is an event listener method used to bind a click event to the "Click" button. Every time the button is clicked, the change method will be executed with an argument of '1'.
Example 2: Type the following code to generate the component for this example:
ember g component <project-name>
@app/templates/components/<project-name>.hbs
HTML
<form {{on "submit" this.submitForm}}>
<div>
<label>
Name :
<input type="text" name="name" />
</label>
</div>
<div>
<label>
Email :
<input type="email" name="email" />
</label>
</div>
<div>
<label>
City :
<input type="text" name="text" / </label>
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
@app/components/<project-name>.js
JavaScript
import Component from '@glimmer/component';
import { action } from '@ember/object';
export default class CounterComponent extends Component {
@action submitForm() {
alert("The form has been submitted");
}
}
Output:
basic form application
Reference: https://api.emberjs.com/ember/4.11/classes/Evented/methods/on?anchor=on
Similar Reads
Node.js emitter.eventNames() Method
In Node.js, most of the core APIs are built near around an idiomatic asynchronous and event-driven architecture. EventEmitter class has instances that are emitted as events by all objects, in which these objects expose an eventEmitter.on() function. It is necessary to import events library (require(
2 min read
Ember.js Ember.Templates.helpers on() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. C
3 min read
Backbone.js on Event
Backbone.js on Event is used to bind a callback function to an object. When an event is triggered, the callback is invoked. If there are large numbers of events on a page then the convention is to use colons to namespace each event: "poll:start", or "change:selection"Syntax: object.on(event, callbac
2 min read
Backbone.js listenToOnce Event
Backbone.js listenTo Event notifies an object to listen to a particular event on another object. The benefit of using this form is that listenTo permits the object to keep the track of the events, & later, they are removed all at once. When an event occurs, the callback function will be called w
2 min read
jQuery event.isDefaultPrevented() Method
jQuery isDefaultPrevented() Method is an inbuilt method that checks whether the preventDefault() method was called for the event. This method returns a boolean value. It returns True if preventDefault() is called on the event, False otherwise. Syntax:event.isDefaultPrevented()Parameters: It accepts
2 min read
jQuery Event Methods Complete Reference
An event refers to the actions performed by the site visitor during their interactivity with the website (or webpage). There can be various types of events such asThe user clicks on the button.The user moves the mouse pointer over an image.The user pressed any key from the keyboard, etc.Syntax:$(sel
6 min read
jQuery click() Method vs onClick Event
jQuery is the JavaScript library known for its lightweight, fast, and cross-platform features. It is an open-source library that goes by the motto "Write less, do more". The main objective of jQuery is to make Java script so easy that one can create more attractive and interactive websites. jQuery h
5 min read
Backbone.js listenTo Event
Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface. Backbone.js listenTo Event
2 min read
JQuery deferred.notify() method
This deferred.notify() method in JQuery is used to call the progressCallbacks on a Deferred object with the given parameters. Syntax: deferred.notify(params)Parameters: params: This is optional parameters which are passed to the progressCallbacks. Return Value: This method method returns the deferre
2 min read
jQuery on() Method
The jQuery on() is an inbuilt method that is used to attach one or more event handlers for the selected elements and child elements in the DOM tree. The DOM (Document Object Model) is a World Wide Web Consortium standard. This method defines accessing elements in the DOM tree. Syntax: $(selector).on
2 min read