worldwidejamie
worldwidejamie

Reputation: 235

Binding Paper-Tabs to Core-Pages with Polymer

I'm building a site with Polymer that uses paper-tabs and core-pages. The problem I'm running into is that I can not seem to get the click event for the tabs to affect the pages being shown and all content remains hidden unless I specifically select which page I want shown.
So I really just want the tabs to behave the way tabs are supposed to behave.

Here is my code so far:

<body unresolved>

  <paper-tabs selected="0" selectedindex="0"  id="paper-tabs"  >

    <paper-tab id="paper-tab" active>ABOUT</paper-tab>
    <paper-tab id="paper-tab1">PORTFOLIO</paper-tab>
    <paper-tab id="paper-tab2">CONTACT</paper-tab>

  </paper-tabs>



  <core-pages selected="{{$.paper-tab.selected}} " selectedindex="0" notap  id="core-pages">
    <about-me id="paper-tab" active>
      <h2 horizontal center-justified>Worldwide Jamie</h2>
      <p>Jamie is a Chicago-based freelance front end web developer.</p>
      <p>Clearly this website is <b>Under Development</b></p>
      <p>Come back soon to see how great your site could be</p>
    </about-me>

    <portfolio-list id="portfolio">
    <!--Insert slider?-->
    </portfolio-list>

    <contact-me id="contact">
    </contact-me>
  </core-pages>    

    </body>
</html>

Thanks in advance for any time and consideration.

Upvotes: 14

Views: 11274

Answers (6)

Charles Clayton
Charles Clayton

Reputation: 17986

As of Polymer 1.0+, this is what you'll want to be using.

<link rel="import" href="components/paper-tabs/paper-tabs.html">
<link rel="import" href="components/iron-pages/iron-pages.html">

<paper-tabs selected="0">
    <paper-tab>Tab One</paper-tab>
    <paper-tab>Tab Two</paper-tab>
</paper-tabs>

<iron-pages selected="0">
    <div>Page One</div>
    <div>Page Two</div>
</iron-pages>

<script>
     var pages = document.querySelector('iron-pages');
     var tabs = document.querySelector('paper-tabs');

    tabs.addEventListener('iron-select', function() { 
        pages.selected = tabs.selected;
    });
</script>

Upvotes: 14

DonSinDRom
DonSinDRom

Reputation: 271

Demo - Codepen.

<polymer-element name="my-element">
  <template>
    <style>
    /* some css */
    </style>

    <section layout vertical is="auto-binding">
      <paper-tabs selected="{{ selected }}" selectedindex="0" horizontal center layout>
        <paper-tab inline flex center-center horizontal layout active>First</paper-tab>
        <paper-tab inline flex center-center horizontal layout>Second</paper-tab>
        ...
      </paper-tabs>
      <core-animated-pages selected="{{ selected }}" selectedindex="0" notap>
        <section active one flex vertical layout>
            <--some html-->
        </section>
        <section one flex horizontal layout>
          <--some html-->
        </section>
        ...
      </core-animated-pages>
    </section>
  </template>

  <script>
    Polymer({
      selected: 0
    });
  </script>
</polymer-element>

<my-element></my-element>

Upvotes: 6

actionless
actionless

Reputation: 198

<template is="auto-binding">

   ...

  <paper-tabs selected="{{selected}}" selectedIndex="0"  id="paper-tabs"  >

   ...

  <core-pages selected="{{selected}}" notap  id="core-pages">

   ...

</template>

and, to add to some other things mentioned in other answers — i think paper-tab element shouldn't have active attribute

Upvotes: 1

Richik SC
Richik SC

Reputation: 884

Simple. use this in your script.

var tabs = document.querySelector('paper-tabs');
var pages = document.querySelector('core-pages');

tabs.addEventListener('core-select',function(){
  pages.selected = tabs.selected;
});

Upvotes: 8

Dirk Grappendorf
Dirk Grappendorf

Reputation: 3422

  • There's a typo in your core-pages 'selected' attribute expression: paper-tab instead of paper-tabs

  • There's a trailing space behind this expression

  • You cannot use paper-tabs as a property name. Rename the paper-tabs id to paperTabs (btw. is there a way to make Polymer print an error message in case of a malformed expression?)

  • As @dfreedm said, you cannot use data binding outside a polymer-element. Another option is: put your whole app into a polymer-element.

Upvotes: 3

dfreedm
dfreedm

Reputation: 834

The binding on <core-pages> is not being evaluated because data binding is only set up for definitions inside of a <polymer-element> template.

Polymer has a special "auto-binding" template that is meant to be put in the main page and provide data-binding for top level elements.

More info: http://www.polymer-project.org/docs/polymer/databinding-advanced.html#bindingoutside

Upvotes: 3

Related Questions