Skip to content

Subview replaces container rather than become child #87

@mikehedman

Description

@mikehedman

The docs state:

container {String} Selector that describes the element within the view that should hold the subview
To me, that indicates that the container will remain, but have a new child once the subview is rendered. But this is not happening. Instead, the action() function in _parseSubview() calls render() which calls renderWithTemplate(), which then replaces the container with the subview:

var parent = this.el && this.el.parentNode;
if (parent) parent.replaceChild(newDom, this.el);

Here's a sample view that illustrates the issue:

var View = require('ampersand-view');
module.exports = View.extend({
    pageTitle: 'tester',
    template:[
        '<div class="root">',
            '<div class="one">',
                '<div class="two"></div>',
            '</div>',
        '</div>'
    ].join(''),
    subviews: {
        whoWeAre: {
            container: '.two',
            constructor: View.extend({
                template: '<p>Mike</p>'
            })
        },
        whatWeDo: {
            container: '.two',
            constructor: View.extend({
                template: '<p>Write code</p>'
            })
        }
    }
});

The intent is to build a page with a series of subviews that all get put into the same container. But the resulting html is not what one would expect:

<div class="root active">
  <div class="one">
    <p>Mike</p>
  </div>
</div>

Rather than my container div still being there with two children, instead only the first child exists, and the second child does not get rendered because it can't find its parent.

I think the renderSubview() function has the right idea - it uses appendChild rather than replaceChild. But re-routing that action() function to use renderSubview didn't work for me because it eventually calls renderWithTemplate - and since it can find a parent it calls replaceChild.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions