Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js - Propagate event up the controller/router chain

In Ember.js, events that aren't handled in the controller propagate up the router chain to the application route (see http://emberjs.com/guides/views/handling-events/ for more details).

Is there a way to define an event handler in the controller that allows the event to continue propagating to the router?

App.SampleController = Ember.ObjectController.extend({
  myEvent: function(obj) {
    this.set('aVariable', true);
  }
});

App.ApplicationRoute = Ember.Route.extend({
  events: {
    myEvent: function(obj) {
      this.transitionTo('something');
    }
  }
});

Is there a way for the myEvent handler in CarsController to set its internal state variable but also kick the event up the chain to the application route? I'm aware of this:

App.__container__.lookup('router:main').send('myEvent', obj);

but it uses Ember internals. I was hoping there was a more standard way to do this.

like image 681
kleinsch Avatar asked Jul 19 '13 16:07

kleinsch


People also ask

What is an ember controller?

In Ember. js, controllers allow you to decorate your models with display logic. In general, your models will have properties that are saved to the server, while controllers will have properties that your app does not need to save to the server.

How does Ember js work?

Ember uses templates to organize the layout of HTML in an application. Ember templates use the syntax of Handlebars templates. Anything that is valid Handlebars syntax is valid Ember syntax. Here, {{name}} is a property provided by the template's context.

Is Ember js client side?

Ember is a client side framework, primarily used to write Single Page Applications for the Web platform.


1 Answers

One way to achieve this could be:

App.SampleController = Ember.ObjectController.extend({
  needs: 'application',
  myEvent: function(obj) {
    this.set('aVariable', true);

    this.get('controllers.application').send('myEvent');
  }
});

Working demo.

Hope it helps.

like image 128
intuitivepixel Avatar answered Sep 20 '22 17:09

intuitivepixel