[dom

编程入门 行业动态 更新时间:2024-10-24 04:41:31
本文介绍了[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

    本文发布于:2023-08-02 11:27:30,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1277428.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:dom

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!