Reputation: 79
I'm new to Ember and I'm having an issue with understanding exactly where "reusable" code should go. From what I've found so far, it sounds like this is supposed to be a utility? The only issue I have no idea how to include the class (or any reusable methods) into the controller.
Here's what I'm after
Example class (where would this go?):
'use strict'
class Person{
constructor(name, age){
this.name = name;
this.age = age;
}
getPerson(){
return "My name is " + this.name + " and I'm " + this.age + " years old";
}
}
/app/routes/index.js:
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel(){
this.replaceWith('rentals');
}
});
/* How would I include the class so I could call the below??
var person1 = new Person('Bob', '15');
var person2 = new Person('Kevin', '41');
console.log(person1.getPerson());
console.log(person2.getPerson());
*/
Upvotes: 0
Views: 516
Reputation: 42460
Create a separate file for your Person
class and import it where you need it. Then you can work with your class as you normally would:
// /app/util/Person.js
export default class Person{
constructor(name, age){
this.name = name;
this.age = age;
}
getPerson(){
return "My name is " + this.name + " and I'm " + this.age + " years old";
}
}
// /app/routes/index.js
import Ember from 'ember';
import Person from '../util/Person';
// ...
const person1 = new Person('Bob', '15');
// ...
Upvotes: 2