Martin
Martin

Reputation: 2300

Accessing itemControllers from parent controller

Is it possible to access a collection of itemControllers from a parent controller? I have the following code:

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>

Upvotes: 1

Views: 360

Answers (1)

Marcio Junior
Marcio Junior

Reputation: 19128

Instead of use itemController="shoppinglistItem" in the each view helper. Move this to App.ShoppinglistController. Like the following:

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>

Upvotes: 1

Related Questions