Need to pass a value from component template to component


#1

I am trying to implement a modal dialog, which consists of a handlebar component (contains title and close button) and a few handlebar templates (contain the actual content of the dialog). I am using ember-i18n to manage the localisation. I now would like to define the title of the dialog in the handlebar template, which should be passed to the component template, where it should be translated. If I could pass the translated title from the handlebar template to the component, that would be equally fine.

The problem is that I seem to be unable to pass the values around as I need:

  • I can pass the value from the view to a controller and translate it there. However, I can’t pass the value from the controller back to the component template.
  • The other way around: I can pass a value from the component to the component template. However I can’t get the variable from the template within the component.

This is what I currently have:

templates/components/modal-dialog.hbs: (the translated title needs to be inserted here)

<div class="overlay" {{action "close"}}>  </div> 
<div class="modal">
   <div class="title"> {{title}}</div>   
   <div>{{yield}</div>   
</div>

templates/modal.hbs: (title variable keeps the identifier for the translation)

{{#modal-dialog action="close" title="modal.title"}}
  <h3 class="flush--top">Name Change Modal</h5>
{{/modal-dialog}}

components/modal-dialog.js: (this works fine, the title variable keeps the translated String. However, I would need to get the ‘title’ variable from the component template, which I can’t get to work properly. If I use something similar in a controller I am able to access the ‘title’ variable, but I am no longer able to access the translated attribute within the component template. It just ends up being empty)

export default Ember.Component.extend(Em.I18n.TranslateableProperties, {
  actions: {
    close: function() {
      return this.sendAction();
    }
  },
  titleTranslation: 'modal.title'
});

Is there any way to do what I want?


#2

Not sure if I’m understanding this perfectly, but is the main problem that you’re dealing with a naming collision on title? Looking at the examples in the Ember-18n readme I’m wondering if passing in the title attribute as titleTranslation is what you want.

{{#modal-dialog action="close" titleTranslation="modal.title"}}
  <h3 class="flush--top">Name Change Modal</h5>
{{/modal-dialog}}

#3

I don’t think this is about naming collisions. I have tried this with various combinations without the translation part in-between. I always end up at the same two issues:

If I use a component and try something like

this.get('title');

then there is no method called ‘get’ within this. I could not find any other way to get the title value. If I just use title without get, it is an actual element in the DOM and not the value passed from the component-template.

If I use a controller I can use the statement above to get the title, however I can’t access any variable I define within the controller from the compnent-template. The variables I print within the template are always empty.


#4

I know this is a pain, but is there any way you could setup a JSBin? I feel like what you’re trying to do is definitely possible.


#5

I agree on the statement about the pain :smiley: I actually could not get the controller part to work the same way it does with ember CLI. A controller with the name of the component produces an error with the JSBin (in case dashes are used for the name) or it never gets called (in case no dashes are used). Not sure how to get this working.

However, it shows the problem with the component. I can define a value ‘modalDialogComponentTitle’ within the component and render it. But I can’t access the raw ‘title’ value as it is defined in the handlebar template


#6

Does this help clear things up?

In short I think you should do this in your template:

{{#modal-dialog action="close" titleTranslation="modal.title"}}
  <h3 class="flush--top">Name Change Modal</h5>
{{/modal-dialog}}

Make sure that there is a translation defined for “modal.title”. Mix-in the library in your component:

export default Ember.Component.extend(Em.I18n.TranslateableProperties, {
  actions: {
    close: function() {
      return this.sendAction();
    }
  }
});

And then just reference title in your template.

<div class="overlay" {{action "close"}}>  </div> 
<div class="modal">
   <div class="title"> {{title}}</div>   
   <div>{{yield}</div>   
</div>

#7

Thanks a lot. That’s less code than what I was trying to write.

I somehow just never tried to add the ‘Translation’ suffix the property in the component template. But that makes things a lot clearer now.