Ember.js EmberObject toString() Method
Last Updated :
27 Dec, 2022
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. Currently, it is utilized by a large number of websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.
The toString() method is used to get the string representation of the object.
Syntax:
toString()
Parameters: It doesn’t take any parameters.
Returns: The string version of the object.
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
To start the server, type:
ember serve
Example 1: Type the following code to generate the route for this example:
ember generate route details
app/routes/details.js
Javascript
import Route from '@ember/routing/route' ;
import EmberObject from '@ember/object' ;
const Person = EmberObject.extend({
toStringExtension() {
return this .get( 'name' );
}
});
export default class DetailsRoute extends Route {
person;
createPerson() {
let person = Person.create({
name: 'Anubhav' ,
mobile: '1298119967' ,
age: 20,
salary: 10000,
city: 'Patna' ,
country: 'India' ,
gender: 'M' ,
zipCode: '800020' ,
});
return person;
}
changeDetails(person) {
person.setProperties({
age: 51,
country: 'England' ,
});
}
model() {
this .person = this .createPerson();
this .changeDetails( this .person);
return this .person;
}
setupController(controller, model) {
super .setupController(controller, model);
controller.set( 'person' , this .person);
}
}
|
app/controllers/details.js
Javascript
import Ember from 'ember' ;
export default Ember.Controller.extend({
actions: {
clickButton() {
alert( this .person.toString());
}
},
});
|
app/template/details.hbs
HTML
{{page-title "Details"}}
< h1 >Person</ h1 >
< div >Name: {{this.person.name}}</ div >
< div >Age: {{this.person.age}}</ div >
< div >Salary: {{this.person.salary}}</ div >
< div >Gender: {{this.person.gender}}</ div >
< div >Zip Code: {{this.person.zipCode}}</ div >
< div >Country: {{this.person.country}}</ div >
< div >Mobile: {{this.person.mobile}}</ div >
< br />< br />
< input type = "button" id = "click"
value = "Click Here!" {{action 'clickButton'}}/>
{{outlet}}
|
Output:
Example 2: Type the following code to generate the route for this example:
ember generate route students
app/routes/students.js
Javascript
import Route from '@ember/routing/route' ;
import EmberObject from '@ember/object' ;
const Student = EmberObject.extend({
toStringExtension() {
return `${ this .get( 'name' )} got ${ this .get( 'marks' )} marks`;
}
});
export default class StudentsRoute extends Route {
student;
createStudent() {
let student = Student.create({
name: 'Alix Mainston' ,
gender: 'F' ,
class: 9,
grade: 'B' ,
marks: 98,
pocket_money: 9643,
});
return student;
}
model() {
this .student = this .createStudent();
return this .student;
}
setupController(controller, model) {
super .setupController(controller, model);
controller.set( 'student' , this .student);
}
}
|
app/controllers/students.js
Javascript
import Ember from 'ember' ;
export default Ember.Controller.extend({
actions: {
clickButton() {
alert( this .student.toString());
}
},
});
|
app/template/students.hbs
HTML
{{page-title "Students"}}
< h1 >Details:</ h1 >
< div >Name: {{this.student.name}}</ div >
< div >Gender: {{this.student.gender}}</ div >
< div >Class: {{this.class}}</ div >
< div >Grade: {{this.student.grade}}</ div >
< div >Marks: {{this.student.marks}}</ div >
< div >Pocket Money: {{this.student.pocket_money}}</ div >
< br />< br />
< input type = "button" id = "click"
value = "Click Here!" {{action 'clickButton'}}/>
{{outlet}}
|
Output:
Reference: https://api.emberjs.com/ember/4.8/classes/EmberObject/methods
Share your thoughts in the comments
Please Login to comment...