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?
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}}
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.
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.
I agree on the statement about the pain 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
http://emberjs.jsbin.com/lidisumo/1/edit?html,css,js,output
Does this help clear things up?
http://emberjs.jsbin.com/lidisumo/11/edit
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>
1 Like
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.