(POLYMER)core-submenu url使用flatiron-director进行路由

时间:2015-02-25 01:16:00

标签: routing polymer

我正在使用Polymer构建一个由core-ajax和WordPress JSON API支持的单页面网站......

我想知道如何为我的core-submenu项目实现url路由?

我已经在我的主要部分网址上使用了flatiron-director,但我不知道如何将它应用到我的子菜单......

开发。网址:http://winkstrategies.com/impactory_dev/

感谢您的帮助!

.. ..源

app / INDEX.HTML:

<!doctype html>
<html lang="fr">

<head>
  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>IMPACTORY</title>
  <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->

  <!-- build:css styles/main.css -->
  <link rel="stylesheet" href="styles/main.css">
  <!-- endbuild-->

  <!-- build:js bower_components/webcomponentsjs/webcomponents.min.js -->
  <script src="bower_components/webcomponentsjs/webcomponents.js"></script>
  <!-- endbuild -->

  <!-- will be replaced with elements/elements.vulcanized.html -->
  <link rel="import" href="elements/elements.html">
  <!-- endreplace-->
</head>

<body unresolved fullbleed layout vertical>
      <template is="auto-binding" id="app">

        <!-- Route controller. -->
        <flatiron-director route="{{ route }}" autoHash></flatiron-director>

        <!-- Keyboard nav controller. -->
        <core-a11y-keys id="keys" target="{{ parentElement }}"
                        keys="up down left right"
                        on-keys-pressed="{{ keyHandler }}"></core-a11y-keys>

        <!-- NAV -->            
        <core-menu selected="{{ route }}" valueattr="data-category" on-core-select="{{ menuItemSelected }}">
          <core-submenu data-category="home" label="HOME">
              <core-item label="Topic 1"></core-item>
              <core-item label="Topic 2"></core-item>
          </core-submenu>
          <core-submenu data-category="pricing" label="PRICING">
              <core-item label="Topic 1"></core-item>
              <core-item label="Topic 2"></core-item>
          </core-submenu>
          <core-submenu data-category="contact" label="CONTACT">
              <core-item label="Topic 1"></core-item>
              <core-item label="Topic 2"></core-item>
          </core-submenu>
        </core-menu>

        <!-- PAGES --> 
        <div class="container">
          <core-animated-pages id="pages" transitions="slide-from-right" selected="{{ route }}" valueattr="data-category">
            <section data-category="home" layout vertical center-center>
              <core-ajax  id="ajax-home"
                          url="//winkstrategies.com/impactory-api/wp-json/posts?type=home-contents"
                          handleAs="json"
                          response="{{ homeContents }}">
              </core-ajax>
              <p>HOME</p>
              <home-page contents="{{ homeContents }}"></home-page>
            </section>
            <section data-category="pricing" layout vertical center-center>
              <core-ajax  id="ajax-pricing"
                          url="//winkstrategies.com/impactory-api/wp-json/posts?type=pricing-contents"
                          handleAs="json"
                          response="{{ pricingContents }}">
              </core-ajax>
              <p>PRICING</p>
              <pricing-page contents="{{ pricingContents }}"></pricing-page>
            </section>
            <section data-category="contact" layout vertical center-center>
              <core-ajax  id="ajax-contact"
                          url="//winkstrategies.com/impactory-api/wp-json/posts?type=contact-contents"
                          handleAs="json"
                          response="{{ contactContents }}"
                          on-core-complete="{{ logContents }}">
              </core-ajax>
              <p>CONTACT</p>
              <contact-page contents="{{ contactContents }}"></contact-page>
            </section>
          </core-animated-pages>
        </div>
    </template>

  <!-- build:js scripts/app.js -->
  <script src="scripts/app.js"></script>
  <!-- endbuild-->
</body>

</html>

app / scripts / APP.JS:

(function () {
  'use strict';

  document.addEventListener('polymer-ready', function () {
    console.log('Polymer is ready to rock!');

    localStorage.clear(); // clear loaded pages status
  });

  // bindings
  var app           = document.querySelector('template[is="auto-binding"]'),
      DEFAULT_ROUTE = 'home';
  app.pages         = [ 'HOME', 'PRICING', 'CONTACT' ];

  app.addEventListener( 'template-bound', function(e) {
      // query selectors
      var pages = document.querySelector( '#pages' ),
          keys  = document.querySelector( '#keys' ),

      // Allow selecting pages by num keypad.
      keysToAdd = Array.apply( null, app.pages ).map( function(x, i) {
        return i + 1;
      }).reduce( function(x, y) {
        return x + ' ' + y;
      });
      keys.keys += ' ' + keysToAdd;

      this.route = this.route || DEFAULT_ROUTE; // Select initial route.
  });

  // key handler
  app.keyHandler = function(e, detail) {
      // Select pages by num key or arrows.
      var num = parseInt( detail.key );
      if ( !isNaN( num ) && num <= this.pages.length ) {
        pages.selectIndex( num - 1 );
        return;
      }
      switch ( detail.key ) {
        case 'left':
        case 'up':
          pages.selectPrevious();
          break;
        case 'right':
        case 'down':
          pages.selectNext();
          break;
      }
  };

  // selected page events
  app.menuItemSelected = function(e, detail, sender) {
      if (detail.isSelected) {
        // dynamic core-ajax firing
        var selectedCategory = detail.item.dataset.category,
            selectedAjax     = selectedCategory;
            selectedAjax     = document.querySelector( '#ajax-' + selectedAjax );
        // load content only once    
        if ( !localStorage.getItem( selectedCategory ) ) {
            localStorage.setItem( selectedCategory, true );
            selectedAjax.go();      
        }
      }
  };

  app.logContents = function(e, detail, sender) {
      console.log(this.contactContents);
  };

})();

1 个答案:

答案 0 :(得分:0)

core-submenu etl al已被弃用,可以使用纸质菜单等。 无论使用何种路由器(即flatiron-director,Excess Router等),子菜单路由都可以以类似的方式解决。

paper-submenu routing目前有局限性:

一种解决方法是通过点击处理程序以编程方式管理菜单/子菜单的状态(从上面链接中的相关答案复制):

<dom-module id="menu-nav">
    <template>
        <!-- define route for 'view', e.g. /:view -->
        <!-- define route for 'subview', e.g. /bar/:subview -->

        <paper-menu selected="{{view}}" attr-for-selected="name">
            <paper-item name="foo" on-tap="_onFoo">
                Foo
            </paper-item>

            <paper-submenu opened="{{_isSubmenu(view, _inSubmenu)}}">
                <paper-item on-tap="_onSubmenu" class="menu-trigger">
                    Bar
                </paper-item>

                <paper-menu selected="{{subview}}" attr-for-selected="subname" class="menu-content">
                    <paper-item subname="baz" on-tap="{{_onBar}}">
                        Baz
                    </paper-item>
                <paper-menu>
            </paper-submenu>
        <paper-menu>
    </template>

    <script>
        Polymer({
            is: "menu-nav",

            properties: {
                view: {
                    type: String,
                    value: "foo"
                },

                _inSubmenu: {
                    type: Boolean,
                    value: false
                }
            },

            _isSubmenu: function (view, _inSubmenu) {
                return _inSubmenu || view === 'bar';
            },

            _onFoo: function () {
                this.set('_inSubmenu', false);
                // ... transition to next route, this could depend on the router being used
                // for eample, with Excess router, this would be:
                // Excess.RouteManager.transitionTo('/foo');
            },

            _onBar: function () {
                this.set('_inSubmenu', true);
                this.set('subview', undefined);
                // ... transition to /bar/baz
            },

            _onSubmenu: function (e) {
                e.stopPropagation();
                this._onBar();
            },
        });
    </script>
</dom-module>