0

Right now I have an Ember component that generates markup for a modal popup. I'd like to pass some HTML to the component (in the form of component attributes) that will be assigned to the modal's content body. Something like this would be ideal:

Route Template: app/templates/section1.hbs

<div class="content">

    <h1>Section 1</h1>

    {{my-modal myContent}}

</div>

Modal Template: app/components/my-modal/template.hbs

<div id="my-modal">

    <div class="modal-dialog">

        <div class="modal-content">

            <div class="modal-body">

                {{myContent}}

            </div>

        </div>

    </div>

</div>

The content I want to display in the modal's body is static and specific to the current template.

I don't want to have this myContent html set in a controller, if possible, or in the route's model hook.

UPDATE: Additionally, would also prefer not to use Block params/syntax as I have so much HTML that would need to go between the blocks, that it would interrupt the flow of the main template markup.

<p>Something in my template worthy of an informational modal {{#my-modal icon=icon}}
    //A ton more markup here to be added to modal body
{{/my-modal}. More text in my template's markup ...</p>

Ideally, I could just set this html in the route's template. Currently this is my workaround that requires appending a javascript template to the desired element after didInsertElement.

Is there a better way to accomplish this, like setting a local handlbars template variable to some html (inside app/templates/section1.hbs)?

Current Solution/Workaround:

Route Template: app/templates/section1.hbs

<script type="text/template" id="my-content">

    <h1>Hello Ember!</h1>
    <p> Welcome to components </p>

</script>

<div class="content">

    <h1>Section 1</h1>

    {{my-modal}}

</div>

Component JS: app/components/my-modal/component.js

import Ember from 'ember';

export default Ember.Component.extend({

    tagName:    'div',
    classNames: 'modals',

    didInsertElement: function() {

        this.$('#my-modal .modal-body').append($('#my-content').html());

    };
});

Modal Template: app/components/my-modal/template.hbs

<div id="my-modal">

    <div class="modal-dialog">

        <div class="modal-content">

            <div class="modal-body">

                //jQuery will append template content here

            </div>

        </div>

    </div>

</div>
mfink
  • 1,309
  • 23
  • 32

1 Answers1

0

Are you familiar with yielding in a component ?

You could simply try the following.

// templates/components/model-component.hbs

<div id="my-modal">

    <div class="modal-dialog">

        <div class="modal-content">

            <div class="modal-body">

                {{yield}}

            </div>

        </div>

    </div>

</div>

And then using the component as follows:

// routes/application.hbs

{{#modal-component}}

 //your special content here

{{/modal-component}}

With your highly dynamic content, a possible solution could be using the component helper, which allows you to load the relevant component.

You can read about it here . Guides link to follow.

A quick code reference follows

{{component contentView content=myContent}}
TameBadger
  • 1,580
  • 11
  • 15
  • This could work, using Block syntax, but I have so much html to put in the modal that it would interrupt the flow of the main template markup to do it this way (especially when the component just renders as an icon added inside a paragraph like `

    Some text that may need a model context {{my-modal icon=context-icon}}

    ` that will trigger the modal).
    – mfink Apr 27 '16 at 01:35
  • How about using the component helper to load a component dynamically into the modal ? – TameBadger Apr 27 '16 at 01:41
  • This sounds promising, do you have a link? I'm not familiar with a component helper. – mfink Apr 27 '16 at 01:50
  • 1
    Writing it up in an answer for you. – TameBadger Apr 27 '16 at 01:53
  • Not sure if `{{component}}` is what I'm looking for. I know I'll always need the same modal component `{{my-modal}}` and the content I want to pass to it I'd like to specify in the template as its pretty static (wont change). Each template I have will have a different modal text. I like the idea of `partial` but will have to research more and see if its worth creating a separate file for the html i want in the modal. – mfink Apr 27 '16 at 02:21