此问题与:flatiron-director / core-pages SPA with route specific js functions & default route直接相关。我确信解决方案有效,但我对聚合物(和js)缺乏经验,无法在我的环境中确定正确的事件监听器:
如果在聚合物元素中使用flatiron-director,特别是当元素的模板本身不使用时,如何/在何处指定适当的事件监听器来设置默认路由= =“自动绑定”。在这种情况下,要明确的是,导入下面显示的元素的index.html页面确实使用is =“auto-binding”指定了一个模板。
这是显示我尝试传达/实现的内容的元素代码。 flatiron路由工作正常(如果我手动将#itemsList或#itemOpen输入到URL并使用浏览器上一个或下一个按钮),但是当它自己点击index.html时,它不会自动将默认的#itemsList添加到URL: / p>
<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>
答案 0 :(得分:1)
如Jeff所述,使用ready()
生命周期方法作为元素之外的template-bound
事件的元素内部元素。所以......基于上面的例子,它就像在聚合物元素的ready()中包含以下行一样简单:
this.route = this.route || "itemsList"