How to create a generic controller model in ember.js?

Member

by domenico , in category: Javascript , 4 months ago

How to create a generic controller model in ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , 4 months ago

@domenico 

To create a generic controller model in Ember.js, you can follow the steps below:

  1. Create a base controller file: First, create a base controller file in your Ember app. This file will serve as the parent controller for all other controllers.
1
2
3
4
5
6
7
// app/controllers/base-controller.js

import Controller from '@ember/controller';

export default Controller.extend({
  // Your generic controller logic goes here
});


  1. Create specific controllers: Next, create specific controllers that will extend the base controller. These specific controllers will inherit the functionality and properties of the base controller.
1
2
3
4
5
6
7
// app/controllers/posts.js

import BaseController from './base-controller';

export default BaseController.extend({
  // Specific controller logic for handling posts
});


  1. Use the generic controller: In your templates or routes, use the specific controllers you have created. These controllers will have access to the generic functionality defined in the base controller.
1
2
3
4
5
6
7
8
9
// app/routes/posts.js

import Route from '@ember/routing/route';

export default Route.extend({
  model() {
    return this.store.findAll('post');
  }
});


By following these steps, you can create a generic controller model in Ember.js that can be easily extended and reused throughout your application.