Polymer dom-重复数据上下文未设置

Poylmer dom-repeat data-context not set

我正在用 Polymer 创建一个站点,其余 api 作为后端。我使用了很多 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);

两种解决方案:

  1. 使用 Polymer 提供的 array mutation methods 而不是直接改变数组
  2. 通过调用 this.$.template.render() 手动要求模板重新呈现自身(假设您的模板项具有 id="template"