gpt4 book ai didi

javascript - polymer dom-repeat 数据上下文未设置

转载 作者:行者123 更新时间:2023-11-28 18:24:39 26 4
gpt4 key购买 nike

我正在 Polymer 中创建一个网站,并以 REST API 作为后端。我使用了很多 dom-repeats,但是这个特定的 dom-repeats 不起作用。这是日历页面。生成了适量的项目,但它们没有数据上下文...

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="../bower_components/paper-spinner/paper-spinner.html">

<link rel="import" href="calendar-month.html">
<link rel="import" href="shared-styles.html">

<script src="../bower_components/moment/min/moment.min.js"></script>
<script src="../bower_components/moment/locale/nl.js"></script>
<script>
moment.locale('nl');
</script>

<dom-module id="calendar-page">
<template>
<style include="shared-styles"></style>

<template is="dom-if" if="[[loading]]">
<div class="spinner-container">
<paper-spinner active></paper-spinner>
</div>
</template>

<template is="dom-repeat" items="[[months]]" as="month">
<calendar-month name="[[month.name]]" calendar-items="[[month.calendarItems]]"></calendar-month>
</template>

<iron-ajax auto url="corrert.api/url" handle-as="json" debounce-duration="300" last-response="{{calendarItems}}" on-response="handleResponse"></iron-ajax>
</template>
<script>
Polymer({
is: 'calendar-page',
properties: {
months: Array,
calendarItems: Array,
loading: {
type: Boolean,
value: true
}
},
handleResponse: function () {
this.months = []
var now = moment().subtract(1, 'M');
var monthNumbers = [];
for(var i = 0; i < 6; i++){
var nowI = now.add(1, 'M');
monthNumbers.push(nowI.month());

var month = {
name: this.capitalizeFirstLetter(nowI.format('MMMM')),
calendarItems: []
}
this.months.push(month);
}

this.test = this.months[0];

for(var i = 0; i < this.calendarItems.length; i++){
var calendarItem = this.calendarItems[i];
var calendarDate = moment(calendarItem.date);
var monthIndex = monthNumbers.indexOf(calendarDate.month());
if(monthIndex !== -1 && calendarDate.year() >= moment().year()){
this.months[monthIndex].calendarItems.push(calendarItem);
}
}
this.loading = false;
},
capitalizeFirstLetter: function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
});
</script>
</dom-module>

生成了 6 个日历月元素,但名称和日历项目属性未正确填写...如果我只输入项目,它就可以工作,所以它不是日历月元素。如果我在控制台中打印月份数组,一切看起来都应该是......

最佳答案

您需要使用描述的数组变异方法 here .

所以,this.push('months', Month); 而不是 this.months.push(month);

关于javascript - polymer dom-repeat 数据上下文未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39344950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com