This problem occurs constantly in Ember. The way I solve this is to keep track of which publication is βopenβ on the controller, and then each element should be responsible for its own state based on this data. This way, you do not need to manually reset each state of the message every time you switch it.
The canonical source of truth is the controller.
App.IndexController = Ember.ArrayController.extend({ content: null, selectedPost: null // null means no post selected });
We pass this information to the components through the template.
<script type="text/x-handlebars" data-template-name="index"> {{#each post in controller}} {{post-summary post=post selectedPost=selectedPost}} {{/each}} </script> <script type="text/x-handlebars" id="components/post-summary"> <h3 {{action "toggleBody"}}>{{post.title}}</h3> {{#if isShowingBody}} <p>{{{post.body}}}</p> {{/if}} </script>
Now body visibility for a given message can be calculated using the property.
App.PostSummaryComponent = Ember.Component.extend({ post: null, selectedPost: null, isShowingBody: function() { return this.get('selectedPost') === this.get('post'); }.property('selectedPost', 'post'), toggleBody: function() { this.set('selectedPost', this.get('isShowingBody') ? null : this.get('post')); } });
Here's the jsfiddle .
It can be argued that this is not an ideal object-oriented solution, but it has significantly improved the structure and support of my Ember applications. You can implement a wide range of complex list behavior, since each element is responsible for its own state based on a common source of truth.
source share