Ember.js - How to trigger view method from controller?

前端 未结 1 1684
日久生厌
日久生厌 2020-12-23 08:05

I\'m trying to call view method from controller, but no idea how to do this. From view I can easily call controller method like this.get(\'controller\').send(\'method\

相关标签:
1条回答
  • 2020-12-23 08:42

    This sounds like a good use for Ember.Evented. By using event subscription and dispatching you can avoid coupling your view and controller.

    Simply mixin Ember.Evented:

    Controller = Ember.Controller.extend(Ember.Evented)
    

    Now you can call on and trigger methods on your controller, to subscribe to an event and then to kick off the event. So, in your view you might do:

    didInsertElement: function () {
        this.get('controller').on('loginDidFail', this, this.loginFail);
    }
    

    And then in your controller call this.trigger('loginDidFail') to kick off your loginFail view method.

    Remember to remove the handler after the view is dismissed... see the answer below.

    0 讨论(0)
提交回复
热议问题