通过重复创建聚合物滑动页面
Polymer swipe-pages create by repeat
我尝试通过模板重复使用滑动页面。
<swipe-pages>
<template is="dom-repeat" items="{{values}}">
<swipe-page>
<div>
Text to swipe
</div>
</swipe-page>
</template>
</swipe-pages>
在我写的聚合物中
created: function() {
console.log(this);
this.values = [1,2,3];
}
它给我错误
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
我无法让它工作。
也用
ready:function(){this.values=[1,2,3];};
不起作用。在这种情况下,它会抛出它们是 0 页的异常。
我认为模板重复后滑动页面没有收到输入运行。
如果我不是通过模板编写它就可以正常工作..
更新:
这就是所有的聚合物元素。
<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>
如果他们是另一个可以动态变化的聚合物滑动页面元素,我会很高兴知道。
如果他们是 hack 解决方案(比如动态加载所有元素)也可以
谢谢。
快速解决方法是修改 swipe-pages
的源代码。
首先,找到 selectedChanged
函数并在最顶部添加此检查 -
selectedChanged: function (newValue, oldValue) {
if (oldValue === undefined) {
return;
}
if (newValue >= this.pageCount || newValue < 0) {
....
然后,因为resetPositions
和resetWidths
调用的太早,所以要替换下面的
ready: function () {
this.resetPositions();
this.resetWidths();
}
和
attached: function () {
this.async(function () {
this.resetPositions();
this.resetWidths();
}, 5);
确保页面已经通过 dom-repeat
呈现。
最后,确保在 ready
或 attached
中初始化数组。
ready: function() {
console.log(this);
this.values = [1,2,3];
}
应该可以!
请看这个: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>
我尝试通过模板重复使用滑动页面。
<swipe-pages>
<template is="dom-repeat" items="{{values}}">
<swipe-page>
<div>
Text to swipe
</div>
</swipe-page>
</template>
</swipe-pages>
在我写的聚合物中
created: function() {
console.log(this);
this.values = [1,2,3];
}
它给我错误
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
我无法让它工作。
也用
ready:function(){this.values=[1,2,3];};
不起作用。在这种情况下,它会抛出它们是 0 页的异常。
我认为模板重复后滑动页面没有收到输入运行。
如果我不是通过模板编写它就可以正常工作..
更新:
这就是所有的聚合物元素。
<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>
如果他们是另一个可以动态变化的聚合物滑动页面元素,我会很高兴知道。
如果他们是 hack 解决方案(比如动态加载所有元素)也可以
谢谢。
快速解决方法是修改 swipe-pages
的源代码。
首先,找到 selectedChanged
函数并在最顶部添加此检查 -
selectedChanged: function (newValue, oldValue) {
if (oldValue === undefined) {
return;
}
if (newValue >= this.pageCount || newValue < 0) {
....
然后,因为resetPositions
和resetWidths
调用的太早,所以要替换下面的
ready: function () {
this.resetPositions();
this.resetWidths();
}
和
attached: function () {
this.async(function () {
this.resetPositions();
this.resetWidths();
}, 5);
确保页面已经通过 dom-repeat
呈现。
最后,确保在 ready
或 attached
中初始化数组。
ready: function() {
console.log(this);
this.values = [1,2,3];
}
应该可以!
请看这个: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>