mercredi 22 juin 2016

Is it possible to inject a controller or another class in an EmberJS Component?

I'm trying to use a method from one controller inside a component but something must be wrong.

I have the route /calendars with its own controller (controllers/calendars.js) and template (templates/calendars.hbs)

Inside calendars.hbs I have implemented a chain of components with templates for the main screen of my app and for the menu. Inside the menu, I have the final component that should call to a calendars' controller method.

Controller:

import Ember from 'ember';

export default Ember.Controller.extend({
  actions:{
    createCalendar: function(){
      console.log("app/controllers/calendars.js");
    }
  }

});

Route:

import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin,{
  model: function(params) {
    return [
      {id:"1", name:"Calendario de trabajo", shared:false, position: 0},
      {id:"2", name:"Calendario personal", shared:false, position: 1},
    ];
  },

  actions:{
    createCalendar: function(calendar){
      console.log("app/routes/calendars.js");
    }
  }
});

Component:

import Ember from 'ember';

export default Ember.Component.extend({

  calendars: Ember.inject.controller("calendars");

  actions: {
    createCalendar: function(){
      console.log("app/components/menu/calendar-dock-footer.js");
      this.calendars.createCalendar();
    }
  }
});

Templates:

calendars.hbs


   Show me the money! 
  
  


main-template.hbs (component)

...
  
...

main-dock.hbs (component)

...
 
...

calendar-dock-footer.hbs (component with button)

<div class="row ht-calendar-footer-content">
    <button class="btn btn-sm btn-info" >
        <label> Add Calendar</label>
    </button>
</div>

I have tried to pass the action from the calendars.hbs to the component with the button, and it runs but what I need to call the method inside calendars.js from the component with the button.




Aucun commentaire:

Enregistrer un commentaire