[dom-repeat :: dom-repeat]:"items"的预期数组,找到了Object [英] [dom-repeat::dom-repeat]: expected array for `items`, found Object

查看:72
本文介绍了[dom-repeat :: dom-repeat]:"items"的预期数组,找到了Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模板:

<iron-ajax 
        id="ajax" 
        url="backend/api.php?operacion=contenidos&idf=[[datos.id]]&len=[[len]]" 
        handle-as="json" 
        verbose=true 
        last-response={{ajaxResponse}} 
        loading="{{cargando}}"> </iron-ajax>

<template is="dom-repeat" items="[[ajaxResponse]]">

AJAX响应包含以下JSON(正确):

The AJAX response contains the following JSON (correct):

{
"1": [{
    "id": "6",
    "idfolleto": "1",
    "fila": "1",
    "orden": "1",
    "tipo": "carrousel",
    "titulo": "",
    "subtitulo": null,
    "color1": null,
    "color2": null,
    "color_fondo": null
}],
"2": [{
    "id": "7",
    "idfolleto": "1",
    "fila": "2",
    "orden": "1",
    "tipo": "texto-imagenes",
    "titulo": "Texto 1",
    "subtitulo": null,
    "color1": null,
    "color2": null,
    "color_fondo": null
}, {
    "id": "8",
    "idfolleto": "1",
    "fila": "2",
    "orden": "2",
    "tipo": "texto-imagenes",
    "titulo": "Texto 2",
    "subtitulo": null,
    "color1": null,
    "color2": null,
    "color_fondo": null
}],
"3": [{
    "id": "9",
    "idfolleto": "1",
    "fila": "3",
    "orden": "3",
    "tipo": "texto-imagenes",
    "titulo": "Texto 3",
    "subtitulo": null,
    "color1": null,
    "color2": null,
    "color_fondo": null
}]
}

但是我得到一个错误:

[dom-repeat::dom-repeat]:items的预期数组,找到了Object {1: Array[1], 2: Array[2], 3: Array[1]}

[dom-repeat::dom-repeat]: expected array for items, found Object {1: Array[1], 2: Array[2], 3: Array[1]}

为什么? 谢谢!

推荐答案

服务器发送的是大型对象,而不是数组.如果可以控制服务,则应在将对象发送到客户端之前将其转换为服务器端的阵列(效率更高,带宽浪费更少).

The server is sending a large object instead of an array. If you have control of the service, you should convert the object into an array server-side before sending it to the client (more efficient, less wasted bandwidth).

如果您不能(或不想)修改服务,则可以在客户端中执行转换.这样您就可以映射减少数据集,丢弃视图中不需要的数据.

If you can't (or don't want to) modify the service, you could perform the conversion in the client. This gives you the opportunity to map-reduce the dataset, discarding data that is unneeded in your views.

以下是几个选项:

  • 使用观察者 ajaxResponse设置绑定在转发器中的另一个属性(例如,_data).

  • Use an observer on ajaxResponse that sets another property, bound in the repeater (e.g., _data).

// template
<iron-ajax
        url="backend/api.php?operacion=contenidos&idf=[[datos.id]]&len=[[len]]" 
        last-response="{{ajaxResponse}}">
</iron-ajax>

<template is="dom-repeat" items="[[_data]]">...</template>

// script
Polymer({
  properties: {
    ajaxResponse: {
      type: Object,
      observer: '_ajaxResponseChanged'
    },

    _data: Array
  },

  _ajaxResponseChanged: function(r) {
    // get data of type 'texto-imagenes' only
    this._data = Object.keys(r)
                       .map(key => ({key, values: r[key].filter(v => v.tipo === 'texto-imagenes')}))
                       .filter(x => x.values.length);
  },
  ...
});

  • 使用计算属性计算的绑定其中根据ajaxResponse计算数据集.

  • Use a computed property or computed binding which computes the dataset based on ajaxResponse.

    // template
    <iron-ajax
            url="backend/api.php?operacion=contenidos&idf=[[datos.id]]&len=[[len]]" 
            last-response="{{ajaxResponse}}">
    </iron-ajax>
    
    // computed property
    <template is="dom-repeat" items="[[_data]]">...</template>
    
    // OR computed binding
    <template is="dom-repeat" items="[[_computeData(ajaxResponse)]]">...</template>
    
    // script
    Polymer({
      properties: {
        ajaxResponse: Object,
    
        _data: {
          computed: '_computeData(ajaxResponse)'
        }
      },
    
      _computeData: function(r) {
        // get data of type 'texto-imagenes' only
        return Object.keys(r)
                     .map(key => ({key, values: r[key].filter(v => v.tipo === 'texto-imagenes')}))
                     .filter(x => x.values.length);
      },
      ...
    });
    

  • 朋克车

    这篇关于[dom-repeat :: dom-repeat]:"items"的预期数组,找到了Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆