【问题标题】:iron-ajax (Polymer 1.0) on-response event firing twiceIron-ajax(Polymer 1.0)响应事件触发两次
【发布时间】:2018-02-24 03:25:20
【问题描述】:

有没有人遇到过 Iron-ajax 的响应事件为单个请求触发两次?我已经仔细检查过,实际上我正在提交一个请求。这是我的 Iron-ajax 实现(只是一个包装 Iron-ajax 的元素):

<dom-module id="my-ajax">

    <template>
        <iron-ajax id="ajax" auto="{{auto}}" url="{{url}}" method="{{method}}" headers="{{headers}}" body="{{body}}" handle-as="json" content-type="application/json" on-response="responseHandler" on-error="errorHandler" with-credentials></iron-ajax>
    </template>

</dom-module>

// Register the polymer element
Polymer({

    is: 'my-ajax',

    properties: {
        actionDesc: {type: String, value: ""},
        auto: {type: Boolean, value: false},
        body: {type: String, value: null},
        headers: {type: Object, value: null},
        isBusy: {
            // One-way binding setup (i.e. child to host only)
            type: Boolean,
            value: false,
            readOnly: true,
            notify: true
        },
        method: {type: String, value: null},
        user: {type: Object, value: null},
        url: {type: String, value: null}
    },

    generateRequest: function() {
        if (!this.isBusy) {
            // Execute request as it isn't currently busy processing a previous request
            this.isBusy = true;

            this.$.ajax.generateRequest();
        } else {
            // TODO: Queue up this request
        }
    },

    responseHandler: function(e, detail) {
        console.log(this.id + " responseHandler fired!\n"); 
        this.isBusy = false;
        this.fire("handle-response", detail.xhr.response);
    }
});

【问题讨论】:

    标签: polymer polymer-1.0


    【解决方案1】:

    答案:

    我认为您应该从您的 &lt;iron-ajax&gt; 声明中删除 auto 属性及其绑定,如下所示:

    <iron-ajax id="ajax" url="{{url}}" method="{{method}}"
               headers="{{headers}}" body="{{body}}" handle-as="json"
               content-type="application/json"
               on-response="responseHandler"
               on-error="errorHandler" with-credentials></iron-ajax>
    


    我认为问题出在“auto”属性中,Polymer´s documentation 显示了这样的示例:
    <iron-ajax
        auto
        url="http://gdata.youtube.com/feeds/api/videos/"
        params='{"alt":"json", "q":"chrome"}'
        handle-as="json"
        on-response="handleResponse"
        debounce-duration="300"></iron-ajax>
    

    文档说:

    auto {Boolean} 默认:false

    如果为 true,则在 url 或 params 更改时自动执行 Ajax 请求。

    所以,我认为当你添加“auto”属性时,它的值默认自动设置为true,即使你绑定它。这就是为什么我认为你应该删除它。

    对不起我的英语,希望你能理解我。

    【讨论】:

    • 是的,auto 属性(尽管它被绑定到一个 false 的值)似乎会导致问题。删除该属性后,响应事件仅触发一次。谢谢!这是设计使然吗?在我看来,如果它被绑定为 boolean false,这应该不会发生。
    • @sinjins 我认为您必须知道您的 ajax 请求是否必须自动生成(添加“auto”属性)或不(不要添加“auto”属性),无论如何,可能类似于“this .$.IdIronAjax.auto = false" 应该可以将“auto”属性设置为 false。
    【解决方案2】:

    正如Flavio Ochoa 所提到的,auto 属性如果为 true,则如果 urlparams 发生更改,则 iron-ajax 将自动执行请求。

    要使绑定按您希望的方式工作而无需两次调用,您的绑定应在 auto 属性上的 = 之前有 $,如下所示:

    <iron-ajax 
       id="ajax" 
       auto$="{{auto}}" 
       url="{{url}}" 
       method="{{method}}" 
       headers="{{headers}}" 
       body="{{body}}" 
       handle-as="json" 
       content-type="application/json" 
       on-response="responseHandler" 
       on-error="errorHandler" 
       with-credentials>
    </iron-ajax>
    

    Polymer docs开始,如果像这样绑定的auto属性只有在绑定值为真时才会设置

    聚会有点晚了,但希望这对某人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 2016-12-12
      • 2015-08-16
      • 2016-04-22
      相关资源
      最近更新 更多