简体   繁体   English

创建我自己的 Iron-ajax 元素

[英]Create my own iron-ajax element

I want to create my own iron-ajax element, that does what iron-ajax does but a little more, add some headers to the request, at the moment I always add the header via data-binding to the headers property of iron-ajax but I dont want to code that on every element I build, I want the iron-ajax Element to do this by itself.我想创建我自己的 Iron-ajax 元素,它做了 Iron-ajax 所做的但更多一点,向请求添加一些标头,目前我总是通过数据绑定将标头添加到 Iron-ajax 的 headers 属性但我不想在我构建的每个元素上都编写代码,我希望 Iron-ajax 元素自己完成。

Is there a way to extend iron-ajax?有没有办法扩展铁阿贾克斯?

Edit:编辑:

<template>
    <iron-ajax
            auto
            url="[[url]]"
            handle-as="json"
            debounce-duration="1000"
            on-response=""
            headers="[[headers]]"
    ></iron-ajax>
</template>
<script>
    (function() {
        'use strict';

        Polymer({
            is: 'em-ajax',

            properties: {
                headers: {
                    type: Object,
                    computed: 'computeHeaders(token)'
                },
                token: {
                    type: String,
                    notify: true,
                    value: "asdf"
                },
                url: {
                    type: String
                }
            },
            computeHeaders: function() {
                return {'Authorization' : 'Bearer {' +  app.accessToken + '}'};
            },
            handleRequest: function(request) {
                console.log("handling request", request);
                request.header = this.computeHeaders();
            }


        });
    })();
</script>

    <em-ajax
            url="http://api.asdf.safd.sdf/profile"
            on-response="handleProfile"
    ></em-ajax>

How can I handle the on-response?我该如何处理响应式? Passing a String with the method name does not seem to work.传递带有方法名称的字符串似乎不起作用。

The Answer I marked is correct.我标记的答案是正确的。 For my edited question I found a solution:对于我编辑的问题,我找到了一个解决方案:

                this.fire('em-response', {event: event, request: request});

I fire an event in my custom ajax Element, and in the element I use the custom ajax element I added a listener.我在我的自定义 ajax 元素中触发了一个事件,并在我使用自定义 ajax 元素的元素中添加了一个侦听器。 Is this a good solution?这是一个很好的解决方案吗?

You can embed <iron-ajax> into your own <fancy-ajax> component and forward properties in and out between <fancy-ajax> and <iron-ajax> .您可以将<iron-ajax>嵌入到您自己的<fancy-ajax>组件中,并在<fancy-ajax><iron-ajax>之间传入和传出属性。

<dom-module id="fancy-ajax">
  <template>
    <iron-ajax ...></iron-ajax>
  </template>
</dom-module>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM