xiaolingxiao
xiaolingxiao

Reputation: 4885

Could someone provide an example of how to define custom events in Ember.js?

In Ember guides:

http://emberjs.com/guides/view_layer/

below the table of built-in events, an example was given of adding custom events:

App = Ember.Application.create({
  customEvents: {
    // add support for the loadedmetadata media
    // player event
    'loadedmetadata': "loadedMetadata"
  }
});

However, I am still a bit confused as to how exactly to implement a custom event. For example, if I want to map a custom event 'leftArrow' to keycode 37, how would I name it ?

Upvotes: 2

Views: 271

Answers (1)

DF_
DF_

Reputation: 3973

For your kind of event, I wouldn't class it as a 'custom event'. Custom events are types of events that are supported natively by the browser or by jQuery that have not been implemented yet in Ember.

What you want to achieve already exists as a 'key' event and you just need to capture the keycode and respond accordingly. For example:

App.SomeView = Em.View.extend({
    keyDown: function (e) {
        if (e.keycode === 37) {
            // do your stuff or call a function
        }
    }
});

The application create constructor is not the place to do this, and you will want to handle the key events within your views or controllers depending on what you want to do. Take a look at the section on the EmberJs doc page just above custom events. Those are the events you can handle.

Upvotes: 1

Related Questions