简体   繁体   中英

Ember.js - bind events in the controller

I've just started with Ember.js and the thing I still don't understand why events are bound inside the view, like this:

App.ClickableView = Ember.View.extend({
  click: function(evt) {
     alert("ClickableView was clicked!");
  }
});

In JavaScriptMVC and Sencha Touch things like this are handled in the controller. How can, for example, multiple controllers bind to the same event?

The default context for events has changed to the router (a controller) from the view in the latest versions of ember. The event will be handled by the current router (state), like this:

root : Ember.Route.extend({
    index : Ember.Route.extend({
        route : '/',
        connectOutlets : function(router) {
            var ac = router.get('applicationController');
            ac.connectOutlet({
                name : 'topnav',
                outletName : 'topnav'
            });
        },
        click: function(event, context) {
           alert("ClickableView was clicked!");
        }
})

If you had other click events in other states, the app would not be confused because it knows which state/route it is in.

You can handle your events in the view or controller, but it is recommended to handle them in the router.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM