【问题标题】:Using polymer ajax response使用聚合物 ajax 响应
【发布时间】:2015-06-02 08:49:16
【问题描述】:

我创建了以下聚合物元素:

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

<dom-module id="task-list-app">
    <style>
        :host {

        }
    </style>
    <template>
        <iron-ajax auto url="../tasks.json" handle-as="json" on-response="handleResponse"></iron-ajax>
        <template is="dom-repeater" items="{{todos}}">
            <span>hello</span>
        </template>
    </template>
</dom-module>

<script>
    Polymer({
        is: "task-list-app",
        created: function () {
            this.todos = [];
        },

        handleResponse: function (data) {
            this.todos = data.detail.response;
        }
    });
</script>

我通过这样做在我的 index.html 中调用它:

<task-list-app></task-list-app>

我希望对于 todo 数组中返回的每个对象,都会打印一个&lt;span&gt;。但是,当我运行应用程序时,我会在控制台中得到以下输出:

未捕获的类型错误:无法读取未定义的属性“待办事项”

polymer.html line 1001

我不确定这里发生了什么以及如何引用从 ajax 响应返回的数据。

【问题讨论】:

    标签: javascript ajax polymer


    【解决方案1】:

    首先,您用于循环数据的第二个模板应该是“dom-repeat”,而不是“dom-repeat”。其次,您可以直接将 iron-ajax 的响应绑定到您的循环模板。像这样,

    <link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
    
    <dom-module id="task-list-app">
        <style>
            :host {
    
            }
        </style>
        <template>
            <iron-ajax auto url="../tasks.json" handle-as="json" last-response="{{ajaxResponse}}"></iron-ajax>
            <template is="dom-repeat" items="[[ajaxResponse.todos]]">
                <span>{{item.todoItem}}</span>
            </template>
        </template>
    </dom-module>
    
    <script>
        Polymer({
            is: "task-list-app"
        });
    </script>
    

    所以你基本上将 last-response 属性的值直接绑定到你的循环模板。

    【讨论】:

    • 感谢您的回答,我已经设法让它工作了。假设我需要先修改响应数据,如何使用 handleResponse 回调使其工作?
    • 另外,如果我想将一个新对象推送到该数组中,我该怎么做?
    【解决方案2】:

    在墙上撞了几个小时后,我设法解决了这个问题。我创建了自己的元素ajax-service,它有一个名为todos 的公共属性,它是Array。在这个元素中,我使用iron-ajax 元素来进行ajax 调用。

    当 ajax 完成时,会调用一个函数并在 todos 属性上设置响应。我还将键 reflectToAttributenotify 设置为 true。这意味着todos 属性的值会反射回主机节点上的属性,并且它可用于双向绑定(有关更多信息,请参阅here)。

    我的task-list-app元素如下:

    <link rel="import" href="ajax-service.html">
    <link rel="import" href="task-item.html">
    <link rel="import" href="tiny-badge.html">
    
    <dom-module id="task-list-app">
        <style>
            :host {
    
            }
        </style>
        <template>
            <ajax-service todos="{{todos}}"></ajax-service>
            <template is="dom-repeat" items="{{todos}}">
                <task-item task="{{item}}"></task-item>
            </template>
            <div>
                <tiny-badge>[[todos.length]]</tiny-badge> total tasks
            </div>
        </template>
    </dom-module>
    
    <script>
        Polymer({
            is: "task-list-app"
        });
    </script>
    

    还有我的ajax-service 元素:

    <link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
    
    <dom-module id="ajax-service">
        <style>
            :host {
    
            }
        </style>
        <template>
            <iron-ajax auto url="../tasks.json" handle-as="json" on-response="tasksLoaded"></iron-ajax>
        </template>
    </dom-module>
    
    <script>
        Polymer({
            is: "ajax-service",
            properties: {
                todos: {
                    type: Array,
                    reflectToAttribute: true,
                    notify: true
                }
            },
            attached: function () {
                this.todos = [];
            },
            tasksLoaded: function (data) {
                this.todos = data.detail.response;
            }
        });
    </script>
    

    这样做意味着我可以在将 on-response 函数中的数据设置到元素之前对其进行编辑。

    【讨论】:

    • 你真的不想要reflectToAttribute: true,尤其是对于数组或对象数据。它会向您的 DOM 发送大量 JSON 垃圾邮件,并且不需要绑定。
    【解决方案3】:

    您需要在脚本中使用它之前定义属性:

    <script>
      Polymer({
        is: "task-list-app",
        properties: {
          todos: {
            type: Array,
            notify: true
          }
        },
    
        handleResponse: function (data) {
          this.todos = data.detail.response;
        }
      });
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      相关资源
      最近更新 更多