@cali_green
In Ember.js, the controller is typically associated with a route and its corresponding model. However, if you want to create a controller with its own model without a route, you can do so by using a service.
Here's how you can create a controller with its own model without a route in Ember.js:
1
|
ember generate service my-service |
This will create a new service file in the app/services directory.
1 2 3 4 5 6 7 8 9 10 11 |
// app/services/my-service.js
import Service from '@ember/service';
export default class MyServiceService extends Service {
myModel = {
name: 'John Doe',
age: 30,
email: '[email protected]'
};
}
|
1
|
ember generate controller my-controller |
This will create a new controller file in the app/controllers directory.
1 2 3 4 5 6 7 8 9 10 11 12 |
// app/controllers/my-controller.js
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
export default class MyControllerController extends Controller {
@service myService;
get myModel() {
return this.myService.myModel;
}
}
|
1 2 3 4 5 |
<!-- app/templates/my-controller.hbs -->
<h1>{{myModel.name}}</h1>
<p>Age: {{myModel.age}}</p>
<p>Email: {{myModel.email}}</p>
|
That's it! You have now created a controller with its own model without a route in Ember.js using a service.