Search code examples
ember.jsember-simple-auth

Ember: call method on mixin from controller


I'm trying to call the authenticate method in Simple Auth's login-controller-mixin.

Here's my controller:

ApplicationController = Ember.Controller.extend(
  LoginControllerMixin,
  authenticator: 'simple-auth-authenticator:devise'
  actions:
    relayAuthentication: (id, password) ->
      @_actions.authenticate()
)

@_actions.authenticate() does seem to work but my context is getting messed up.

More generally, is this the correct way to call a method that's been mixed in to an Ember controller?


Here's what I'm ultimately trying to do:

I have a component which handles logging in. Here's the template:

{{#if isFormOpen}}

  <div class="row">
    <form class="large-4 columns" {{action 'authenticate' on='submit'}}>
      <label for="identification">Login</label>
      {{input id='identification' placeholder='Enter Login' value=identification}}
      <label for="password">Password</label>
      {{input id='password' placeholder='Enter Password' type='password' value=password}}
      <button type="submit">Login</button>
      <button type="button" {{action 'toggleForm'}}>Cancel</button>
    </form>
  </div>

{{else}}

  {{#if session.isAuthenticated}}
    <a class="button tiny" {{action 'invalidateSession'}}>Logout</a>
  {{else}}
    <a class="button tiny" {{action 'toggleForm'}}>Login</a>
  {{/if}}

{{/if}}

Here's the component itself:

`import Ember from 'ember'`

LoginComponent = Ember.Component.extend(
  actions:
    toggleForm: ->
      @toggleProperty('isFormOpen')
    authenticate: ->
      @sendAction('action', @identification, @password)
)

`export default LoginComponent`

The authenticate action is passed in to the component when it's used ({{login-form action="authenticate"}})

That authenticate method comes from a Simple Auth mixin on the application controller.

I'm not really sure how to give the original authenticate method (from the mixin) what it wants, ie: context.


Solution

  • No, it's not. If it's a method, you just call it in the normal way. If it's an action, you just send it in the normal way.

    actions:
      relayAuthentication: (id, password) ->
        @send 'authenticate', id, password
    

    But in this case, why not simply specify the authenticate action directly in the template instead of going through relayAuthentication?