0
votes

Is there a way to access other controller's action methods from controller?

I've tried something @get('controllers.user').stopEditing() like below but it doesn't seem to be working:

user_edit_controller.js.coffee

App.UserEditController = Ember.ObjectController.extend
  needs: ['user']
  title_options: ["Mr", "Mrs", "Dr", "Miss", "Ms"]

  startEditing: ->
    user = @get 'model'
    transaction = user.get('store').transaction()
    transaction.add user
    @transaction = transaction

  stopEditing: ->
    transaction = @transaction
    if(@transaction)
      @transaction.rollback()
      @transaction = undefined

  actions:
    save: ->
      @transaction.commit()
      @transaction = undefined
      @get('controllers.user').stopEditing()

    cancel: ->
      @get('controllers.user').stopEditing()

user_controller.js.coffee

App.UserController = Ember.ObjectController.extend
  isEditing: false
  needs: ['userEdit']

  actions:
    startEditing: ->
      userEditController = @get 'controllers.userEdit'
      userEditController.set 'model', @get 'model'
      userEditController.startEditing()
      @set 'isEditing', true

    stopEditing: ->
      userEditController = @get 'controllers.userEdit'
      userEditController.stopEditing()
      @set 'isEditing', false

    destroyRecord: ->
      if window.confirm "Are you sure you want to delete this contact?"
        @get('model').deleteRecord()
        @get('store').commit()

        # return to the main contacts listing page
        @get('target.router').transitionTo 'users.index'
2
What does not work exactly? I would suspect that @get('controllers.user.actions').stopEditing() should work. - mavilein

2 Answers

1
votes

To programatically trigger an action you need to use send(actionName, args ...).

In your case just update to the following:

@get('controllers.user').send('stopEditing')
1
votes

You are getting the wrong controller (unless you have the same methods on the user controller).

@get('controllers.userEdit').stopEditing()

but you are in scope of the controller, so you should just be able to do

@stopEditing()

If you really want them to be actions they need to live within the actions hash, and if that's the case and the action lives on this controller, or it's associated route, or a parent's route you can just use

@send('stopEditing')

To see where actions go, scroll down this page a bit, it has a nice diagram: http://emberjs.com/guides/templates/actions/