简体   繁体   English

聚合物滑动页面通过重复创建

[英]Polymer swipe-pages create by repeat

I try to use swipe-pages by template repeat. 我尝试通过模板重复使用滑动页面。

   <swipe-pages>
        <template is="dom-repeat" items="{{values}}">
          <swipe-page>
               <div>
                   Text to swipe
               </div>
          </swipe-page>
         </template>
   </swipe-pages>

In the polymer I wrote 在我写的聚合物中

created: function()  {
        console.log(this);
        this.values = [1,2,3];
       }

It give me the error 它给了我错误

Uncaught TypeError: Cannot set property 'values' of undefined
  Polymer.created   
  Polymer.Base._addFeature._invokeBehavior  
  Polymer.Base._addFeature._doBehavior  
  Polymer.Base.createdCallback  
  window.Polymer    
  (anonymous function)  
  Polymer.DomApi.DomApi._addNode

I cant get it work. 我不能让它工作。

Also use 也用

ready:function(){this.values=[1,2,3];};

does not work. 不起作用。 in this case it throws exception that their is 0 pages. 在这种情况下,它会抛出异常,即它们是0页。

I think that the the swipe-pages does not receive the input after the template repeat run. 我认为在模板重复运行后,滑动页面不会收到输入。

If I write it not by template it works ok.. 如果我不是通过模板写它,它可以正常工作..

update: 更新:

this is all the polymer-element. 这是所有的聚合物元素。

<dom-module id="element-element">
          <template>
              <swipe-pages>
                <template is="dom-repeat" items="{{values}}">
                   <swipe-page>
                     <div>
                        text
                     </div>
                   </swipe-page>
               </template>
              </swipe-pages>
           </template>
           <script>
               Polymer({
                    is:'element-element',
                    created: function()  {
                        this.values = [1,2,3];
                    },
                    ready: function(){
                        this.values=[1,2,3];
                    }
                });

            </script>
</dom-module>

If their is another swipe page element for polymer that can dynamically change I will be happy to know. 如果它们是聚合物的另一个滑动页面元素,可以动态改变,我将很高兴知道。

If their is a hack solution (like load all the element dynamically) it will be also ok 如果他们是一个黑客解决方案(如动态加载所有元素),它也可以

Thanks. 谢谢。

A quick fix is to modify swipe-pages 's source code. 快速修复是修改swipe-pages的源代码。

First, go find the selectedChanged function and add this check at the very top - 首先,找到selectedChanged函数并在最顶部添加此检查 -

selectedChanged: function (newValue, oldValue) {
    if (oldValue === undefined) {
        return;
    }

    if (newValue >= this.pageCount || newValue < 0) {
        ....

Then, because resetPositions and resetWidths are called too early, you want to replace the following 然后,因为resetPositions调用resetPositionsresetWidths ,您想要替换以下内容

ready: function () {
    this.resetPositions();
    this.resetWidths();
}

with

attached: function () {
    this.async(function () {
        this.resetPositions();
        this.resetWidths();
    }, 5);

to make sure the pages are already rendered via dom-repeat . 确保页面已经通过dom-repeat呈现。

Lastly, make sure you initialize the array inside either ready or attached . 最后,确保在readyattached内部初始化阵列。

ready: function()  {
    console.log(this);
    this.values = [1,2,3];
}

That should do it! 应该这样做!

Please have a look at this: argon-swipeable-pages ! 请看看这个: 氩可刷页面

<argon-swipeable-pages items="[[ data ]]" as="person" index-as="pix">
    <template>
        <p>Name: [[ person.name ]]</p>
        <p>Age: [[ person.age ]]</p>
        <p>Index: [[ pix ]]</p>
    </template>
</argon-swipeable-pages>

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

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