Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ember.js | notify other components on one component action

Tags:

ember.js

in the components section at Ember's guides there a demo of Post Summary Component, clicking on one Post Summery title opens its content below it.

I would like to add the functionality to close any other opened Post summaries at the same time.

The purpose on my question is the understand how ember speaks between components without sacrificing isolation.

the solutions I thought about are:

  1. have some wrapper component that handles it in some way

  2. firing an event like 'post-summery:open' and make other components close themselves on it (but then it can Collide with other places on the app using the same components for diffecnt uses)

this is the original demo from the documentation: http://jsbin.com/uyibis/1/edit

This is how I would Implement the behavior with jQuery: http://jsbin.com/eremon/2/edit

var $contents = $('.content').hide();

$(document).on('click', '.title', function () {

$contents.hide();
$(this).next('.content').show();

});

enter image description here

like image 507
Asaf Katz Avatar asked Mar 22 '23 22:03

Asaf Katz


2 Answers

This problem comes up all the time in Ember. The way I solve it is to keep track of which post is "open" on the controller and then have each item's view be responsible for its own state based on that data. That way you don't have to manually reset every post's state each time you toggle one.

The canonical source of truth is the controller.

App.IndexController = Ember.ArrayController.extend({
    content: null,
    selectedPost: null // null means no post selected
});

We wire 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 the body visibility for a given post can be computed by a 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.

One could argue that this isn't an ideal object-oriented solution, but it has drastically improved the structure and maintainability of my Ember apps. You can implement a wide variety of complex list behaviours by having each element responsible for its own state based on a common source of truth.

like image 113
ahmacleod Avatar answered Apr 01 '23 05:04

ahmacleod


With Ember.Component you don't have the control of the childViews, so I used the Ember.CollectionView, to manage the items. Because App.PostSummaryComponent already extends Ember.Component, I delegate to the Ember.CollectionView that behavior using view containerViewClass in the template. So we have the best of the two worlds.

Since that containerViewClass property is a class, we need a way to access that instance - to interact with the childViews - when the view is created. To do this you have to use viewName='containerView' in the template. This tells Ember, that a new instance of containerViewClass will be setted in a property called containerView. So we can use this.get("containerView") in App.PostSummaryComponent.

The last change was moving the each helper from index template to the component template. So others calls to the component doesn't need to repeat it.

http://jsbin.com/ebayip/2/edit

like image 32
Marcio Junior Avatar answered Apr 01 '23 04:04

Marcio Junior