当前位置: 首页 > 面试题库 >

使用聚合物ajax响应

阙阳夏
2023-03-14
问题内容

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

<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数组中返回的每个对象,<span>都会打印出一个。但是,当我运行该应用程序时,我在控制台中得到以下输出:

未捕获的TypeError:无法读取未定义的属性“ todos”

polymer.html line 1001

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


问题答案:

将头撞在墙上几个小时后,我设法解决了这个问题。我创建了我叫自己的元素ajax- service是有一个叫做公共财产todos这是一个Array。在此元素中,我使用该iron-ajax元素进行ajax调用。

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

我的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设置在元素上之前在函数中编辑数据。



 类似资料:
  • 我试图用聚合物表达式来显示某些东西。 我有一个文件,它打印出json_encoded数组,例如名称。在我看来,有三种可能性: null

  • 那些日子我一直在玩polymer,只是为了测试一些关于AJAX调用等的可能性。 由于聚合物的性质,由于DOM/AJAX竞争条件,我面临一些奇怪的行为。 事实上,我有一个自定义元素,它使用ajax调用来检索针对远程服务的信息(JSON)。 以下是组件: 组件模板合作伙伴。html 下面是组件功能。js 我不知道为什么,但如果我只使用1个结果元素,元素正确渲染和刷新尽快得到响应,但是如果我收到两个或更

  • 我试图在某一元素条件下隐藏几个聚合物元素。我知道有几种可能性。在我的opinon中,最简单的方法是引入一个新的CSS类 并将其添加到聚合物元素的类列表中 但这对元素没有影响。元素仍然可见。对elment检查器的查看显示,添加了该类: 其中parent-elem是父元素的名称。 谁能解释一下为什么元素不会被隐藏? 谢谢。 问候你,梅森曼

  • 问题内容: 我正在使用polymer-jsonp执行JSONP请求,但响应有时包含html。 例如,假设post.content为,我如何以粗体显示{{post.content}} ? 问题答案: Polymer不会通过数据绑定标记未转义的HTML,因为它成为XSS攻击的漏洞。 关于在有限的情况下标记HTML或允许进行自定义过滤的讨论正在进行,但是在数据层尚未实现。 现在可以使用附加的自定义元素来

  • 问题内容: 我想在Polymer和Angular之间进行两种方式的数据绑定,为此,我正在使用Angu- poly 库。 这是一个有效示例的链接,同时通过name和age属性传递字符串:working。 但是,当我尝试通过kid属性传递对象时,似乎出现了问题,这是链接:不起作用。 我应该怎么做才能使其正常工作? 更新: 也尝试过使用此库,但无法正常工作。该代码将如下所示: 重大更新: 在这里,我对“

  • 我正在迁移到Polymer 1.0 这是我的模板: 内容将在主html文件中填充文本。 我需要得到这个div的滚动高度。我曾经做过: 但这已经不起作用了: 我尝试向div添加一个id,并按如下方式选择它: 但这给了我一个0的值,即使内容中有很多文本。 我从就绪函数调用此代码。 我选择的元素是否正确?