2013-12-12 99 views
1

是否可以從父控制器訪問itemController的集合? 我有以下代碼:從父控制器訪問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

而不是在每個視圖助手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>