0

有没有办法从控制器访问其他控制器的action方法?

我已经尝试过@get('controllers.user').stopEditing()类似下面的方法,但它似乎不起作用:

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'
4

2 回答 2

1

要以编程方式触发您需要使用的操作send(actionName, args ...)

在您的情况下,只需更新到以下内容:

@get('controllers.user').send('stopEditing')
于 2013-11-10T18:17:56.230 回答
1

您得到了错误的控制器(除非您在用户控制器上有相同的方法)。

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

但你在控制器的范围内,所以你应该能够做到

@stopEditing()

如果您真的希望它们成为动作,则它们需要存在于动作哈希中,并且如果是这种情况并且动作存在于此控制器上,或者它是关联的路由,或者您可以使用父路由

@send('stopEditing')

要查看操作的去向,请向下滚动此页面,它有一个很好的图表:http ://emberjs.com/guides/templates/actions/

于 2013-11-10T19:12:05.570 回答