Javascript 未设置Poylmer dom重复数据上下文

Javascript 未设置Poylmer dom重复数据上下文,javascript,polymer,polymer-1.0,Javascript,Polymer,Polymer 1.0,我正在制作一个以RESTAPI为后端的Polymer网站。我使用了很多dom重复,但这个特殊的不会工作。这是一个日历页。生成了适当数量的项,但它们没有数据上下文 <link rel="import" href="../bower_components/polymer/polymer.html"> <link rel="import" href="../bower_components/iron-ajax/iron-ajax.html"> <link rel="imp

我正在制作一个以RESTAPI为后端的Polymer网站。我使用了很多dom重复,但这个特殊的不会工作。这是一个日历页。生成了适当数量的项,但它们没有数据上下文

<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>

地点('nl');
聚合物({
是:'日历页',
特性:{
月份:数组,
日历项:数组,
装载:{
类型:布尔型,
值:true
}
},
HandlerResponse:函数(){
这个月数=[]
var now=力矩()。减去(1,'M');
var-monthNumbers=[];
对于(变量i=0;i<6;i++){
var nowI=now.add(1,'M');
monthNumbers.push(nowI.month());
var月={
名称:this.capitalizeFirstLetter(nowI.format('MMMM')),
日历项目:[]
}
本.月.推(月);
}
this.test=this.months[0];
对于(var i=0;i=矩().year()){
此.months[monthIndex].calendarItems.push(calendarItem);
}
}
这一点:加载=错误;
},
大写第一个字母:函数(字符串){
返回string.charAt(0.toUpperCase()+string.slice(1);
},
});

生成了6个日历月元素,但名称和日历项属性未正确填充。。。如果我只是键入项目,它会工作,因此它不是日历月元素。如果我在控制台中打印months数组,一切看起来都应该是…

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


所以,
this.push('months',month)而不是
此.months.push(月)

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

所以,
this.push('months',month)而不是
此.months.push(月)

两种解决方案:

  • 使用聚合物提供的基因,而不是直接突变阵列
  • 通过调用
    this.$.template.render()
    (假设模板项具有
    id=“template”
    ),手动要求模板重新呈现自身
  • 两种解决方案:

  • 使用聚合物提供的基因,而不是直接突变阵列
  • 通过调用
    this.$.template.render()
    (假设模板项具有
    id=“template”
    ),手动要求模板重新呈现自身