从父控制器访问itemControllers

时间:2013-12-12 08:30:54

标签: ember.js

是否可以从父控制器访问itemControllers的集合? 我有以下代码:

App.ShoppinglistController = Ember.Controller.extend(
  cartContent: null

  actions:
    removeSelected: ->
      # How to I get each of the cart contents with it's ShoppinglistItemController?
      # I want to filter them by the 'isSelected' property
)

App.ShoppinglistItemController = Ember.ObjectController.extend(
  isSelected: null
)

App.ShoppinglistRoute = Ember.Route.extend(
  setupController: (controller) ->
    # [...]
    controller.set('cartContent', cartContent)
)

<script type="text/x-handlebars" data-template-name="shoppinglist">
  {{#each cartContent itemController="shoppinglistItem"}}
    {{title}}
    {{view Ember.Checkbox checked=isSelected}}
  {{/each}}
  <div {{action 'removeSelected'}}>Remove selected items</div>
</script>

1 个答案:

答案 0 :(得分:1)

而不是在每个视图助手中使用itemController="shoppinglistItem"。将其移至App.ShoppinglistController。如下所示:

App.ShoppinglistController = Ember.Controller.extend(

  itemController: "shoppinglistItem"

  cartContent: null

  actions:
    removeSelected: ->
      cartsToRemove = @.get('cartContent').filterBy('isSelected', true)
      # ... remove the carts
)

App.ShoppinglistItemController = Ember.ObjectController.extend(
  isSelected: null
)

App.ShoppinglistRoute = Ember.Route.extend(
  setupController: (controller) ->
    # [...]
    controller.set('cartContent', cartContent)
)

<script type="text/x-handlebars" data-template-name="shoppinglist">
  {{#each cartContent}}
    {{title}}
    {{view Ember.Checkbox checked=isSelected}}
  {{/each}}
  <div {{action 'removeSelected'}}>Remove selected items</div>
</script>