Search code examples
javascriptpolymercore-elementsflatiron.js

Specifying a default flatiron-director route (inside element) via polymer core-pages component


This question is directly related to: flatiron-director / core-pages SPA with route specific js functions & default route . I'm sure that solution works, but I'm a little too inexperienced with polymer (and js) to determine the correct event listener in my circumstance:

How/where would you specify an appropriate event listener to set the default route if the flatiron-director is used inside a polymer element, particularly when the element's template itself does not use is="auto-binding". In this case, and to be clear, the index.html page which imports the element shown below does in fact specify a template using is="auto-binding".

Here is the element code to show what I am attempting to communicate / achieve. The flatiron routing is working (if I manually enter #itemsList or #itemOpen into the URL and use browsers previous or next buttons), but it does not add the default #itemsList to the URL automatically when hitting index.html on its own:

<polymer-element name="my-app" attributes="user items connected">

<template>

    <flatiron-director id="page-director" route="{{route}}" autoHash on-director-route="{{ routeChanged }}"></flatiron-director>

    <!-- HIGH LEVEL APP LAYOUT ELEMENT -->
    <core-header-panel id="appHeader" mode="standard">

        <!-- OUTER APP TOOLBAR ELEMENT -->
        <core-toolbar id="appToolbar">
            <paper-icon-button id="navicon" icon="arrow-back" on-tap="{{ showItems }}"></paper-icon-button>
            <span flex>App Name</span>
            <paper-icon-button id="searchbutton" icon="search"></paper-icon-button>
        </core-toolbar>

        <!-- MAIN CONTENT ELEMENTS -->

        <!-- ATTEMPT FLATIRON ROUTING -->
        <core-pages id="mainPages" selected="{{route}}" valueattr="name">

            <my-items-element name="itemsList" on-core-activate="{{ itemSelect }}" user="{{user}}" items="{{items}}" item="{{item}}"></my-items-element>

            <item-open-scaffold-element name="itemOpen" user="{{user}}" item="{{item}}" hidden></item-open-scaffold-element>

        </core-pages>


    </core-header-panel>

</template>

<script>
    Polymer('my-app', {

        route: "itemsList",

        itemSelect: function(e, detail, sender) {
            if (sender.shadowRoot.activeElement == null || sender.shadowRoot.activeElement.nodeName != "PAPER-MENU-BUTTON"){
                // Ensure the user hasn't clicked on the item menu dropdown to perform alternative actions (or another element with actions for that matter)
                // (i.e. make sure the user intends to open the item)
                this.openItem();
            }
        },

        openItem: function() {
            this.$.mainPages.children.itemOpen.hidden = false;
            this.$.mainPages.selected = "itemOpen";
            //this.route = "scaffoldPage";
        },

        showItems: function() {
            this.$.mainPages.children.itemOpen.hidden = true;
            this.$.mainPages.selected = "itemsList";
        }
    });
</script>

<script>
    var template = document.querySelector('template');

    template.addEventListener('template-bound', function() {
        this.route = this.route || "itemsList";
    });
</script>


Solution

  • As noted by Jeff, use ready() lifecycle method as intra-element equivalent to template-bound event outside of element. So...based on the example above, its as simple as including the following line within polymer element's ready():

    this.route = this.route || "itemsList"