简体   繁体   English

在聚合物1.0中布线

[英]Routing in polymer 1.0

I am new to web development and building an application using polymer 1.0.4. 我是使用聚合物1.0.4进行Web开发和构建应用程序的新手。 I am using the page.js routing similar to the example in start kit. 我正在使用类似于入门工具包示例中的page.js路由。 Now many of the custom element that I built are using ajax and periodically refresh the data. 现在,我构建的许多自定义元素都使用ajax并定期刷新数据。 The problem with page.js routing that It seems it loads all custom elements even if the element is not viewed by user. Page.js路由的问题是,即使用户未查看该元素,它似乎也会加载所有自定义元素。 so all custom elements are loading the data even if it is not needed. 因此所有自定义元素都在加载数据,即使不需要它也是如此。 my questions: 我的问题:

1- How could I fix this so the the elements load data only when they are viewed by the end users? 1-如何解决此问题,使元素仅在最终用户查看数据时才加载数据? Should I change the routing to another options like more-routing? 我是否应该将路由更改为其他选项,例如更多路由?

2- if the user filled the data in one custom element , then clicked on link to another element. 2-如果用户将数据填充到一个自定义元素中,则单击指向另一元素的链接。 The data will remains when the user goes back to the first custom element? 当用户返回第一个自定义元素时,数据将保留吗? How could I reset the polymer and html elements in the custom element when the use back to an old view? 当使用回到旧视图时,如何重置自定义元素中的polymer和html元素?

Again, I'd recommend https://github.com/PolymerLabs/more-routing Eventually a 'carbon' (if I recall the name correctly) set of components will deal with this, according to the polymer summit videos, but until then this seems the standard approach. 再次,我建议https://github.com/PolymerLabs/more-routing根据聚合物峰会的视频,最终一组“碳素”(如果我没记错这个名字的话)组件将处理这个问题,但是直到那时这似乎是标准方法。

Set up the pages via: 通过以下方式设置页面:

<more-routing-config driver="hash"></more-routing-config>
<more-route name="one" path="/one"></more-route>
<more-route path="/two">
    <more-route name="two" path="/:name"></more-route>
</more-route>

Then the menu via: 然后通过以下菜单:

<more-route-selector>
  <paper-menu selected="0">
    <paper-item route="{{urlFor('one')}}">One</paper-item>
    <paper-item route="{{urlFor('two', {name: 'sup'})}}">Two</paper-item>   
  </paper-menu> 
</more-route-selector>

And then the actual pages via: 然后通过以下方式获取实际页面:

<more-route-selector selectedParams="{{params}}">
  <iron-pages selected="0">
    <section route="one">
      <div> Page one </div>
    </section>
    <section route="two">
      <div> Page two: {{params.name}} </div>
    </section>
  </iron-pages>
</more-route-selector>

I used it when it was under the Polymore repository on github, and the samples above are from such, but it doesn't seem to have changed that much in its new home. 当它在github上的Polymore存储库下时,我使用了它,而上面的示例来自于此,但在它的新家中似乎并没有太大改变。

After you've set up the basics, listen for changes on the iron-pages, such as events that are available here . 设置完基础后,请听听铁页上的更改,例如此处提供的事件。 In such listeners, you can load the data for each section in iron-pages. 在此类侦听器中,您可以在铁页中加载每个部分的数据。 One approach would be to use such listeners to call a method of a custom element, perhaps using a behaviour, that then brings down the data. 一种方法是使用此类侦听器调用自定义元素的方法(可能使用行为),然后关闭数据。

Try dna-router . 尝试dna-router You can create define states and routes in HTML only. 您只能在HTML中创建定义状态和路由。

Setup routes by: 设置路线的依据:

<dna-new-state state='home' route='/home'></dna-new-state> <dna-new-state state='user' route='/user/:id/'></dna-new-state>

Create views by: 通过以下方式创建视图:

<dna-view
state='home'
element='home-template'></dna-view>

You can get all params inside your home-template polymer properties. 您可以在home-template聚合物属性中获取所有params

var params = this.params

For a detailed documentation, visit : https://github.com/Saquib764/dna-router 有关详细文档,请访问: https : //github.com/Saquib764/dna-router

Firstly, the PolymerLabs/more-routing library is a nice alternative to page.js and is quite easy to implement. 首先, PolymerLabs /更多路由库是page.js的不错替代品,并且很容易实现。 As this library is more declarative you can do things like: 由于该库更具声明性,因此您可以执行以下操作:

routes.html routes.html

<more-routing-config driver="hash"></more-routing-config>
<more-route name="myRoute" path="/my-route-path/:id"></more-route>

app-element.html APP-element.html

<dom-module id="app-element">
  <style>
    iron-selector > * {
      display: none;
    }
    iron-selector > .iron-selected {
      display: block;
    }
  </style>

  <template>
    <more-route-selector>
      <iron-selector>
        <x-element></x-element>
      </iron-selector>
    </more-route-selector>
  </template>
  <script>Polymer({ ... });</script>
</dom-module>

x-element.html X-element.html

<dom-module id="x-element">
  <template>
    <more-route id="route" context name="myRoute" params="{{params}}" active="{{activeRoute}}"></more-route>
  </template>
  <script>
    Polymer({
      is: 'x-element',
      observers: [ '_paramsChanged(activeRoute, params.id)' ],

      _paramsChanged: function(activeRoute) {
        if (activeRoute) {
         // Active route
        } else {
         // Inactive route
        }
      }
    })
  </script>
</dom-module>

Check out the polyfora app in the demo folder of the repository. 在存储库的demo文件夹中polyfora应用程序。

Otherwise, to use page.js I would consider: 否则,要使用page.js我将考虑:

  • Remove any auto iron-ajax queries in custom elements; 删除自定义元素中的所有自动iron-ajax查询;
  • Pass a state attribute to custom elements; state属性传递给自定义元素;
  • Add an observer to any state changes within each custom element which triggers a function to run any iron-ajax queries. 将观察者添加到每个自定义元素内的任何state更改中,以触发函数运行任何iron-ajax查询。

As of Polymer 1.4, carbon-route (later renamed app-route) can be used: 从Polymer 1.4开始,可以使用carbon-route(后重命名为app-route):

Here's an example taken from the polymer blog: 这是从Polymer博客中获取的示例:

<carbon-location route="{{route}}">
</carbon-location>

<carbon-route route="{{route}}" pattern="/tabs/:tabName" data="{{data}}">
</carbon-route>

<paper-tabs selected="{{data.tabName}}" attr-for-selected="key">
  <paper-tab key="foo">Foo</paper-tab>
  <paper-tab key="bar">Bar</paper-tab>
  <paper-tab key="baz">Baz!</paper-tab>
</paper-tabs>

<neon-animated-pages selected="{{data.tabName}}"
                     attr-for-selected="key"
                     entry-animation="slide-from-left-animation"
                     exit-animation="slide-right-animation">
  <neon-animatable key="foo">Foo Page Here</neon-animatable>
  <neon-animatable key="bar">Bar Page Goes Here</neon-animatable>
  <neon-animatable key="baz">Baz Page, the Best One of the Three</neon-animatable>
</neon-animated-pages>

See also similar question: Polymer 1.0 - routing 另请参阅类似的问题: Polymer 1.0-路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM