7

在响应来自 Iron-ajax 的情况下,我如何编写 Promise。

<iron-ajax id="listItems" method="GET" content-type="application/json" handle-as="json" last-response="{{items}}" on-error="handleErrorResponse"></iron-ajax>

this.data =  {
    get: function(sort, page, pageSize) {
            return new Promise(function(resolve, reject) {
                // Execute iron-ajax.
                //...
                // resolve(iron-ajax's response);
            });
        }
    }
};

4

1 回答 1

8

您正在寻找listItem.generateRequest(),因为它返回 iron-ajax 随附的 iron-request 对象,该对象又提供了一个名为 的承诺request.completes

https://www.webcomponents.org/element/PolymerElements/iron-ajax/iron-ajax#method-generateRequest

https://www.webcomponents.org/element/PolymerElements/iron-ajax/iron-request#property-completes

我在下面修改了您的代码示例:

<iron-ajax id="listItems" method="GET" content-type="application/json" handle-as="json" last-response="{{items}}" on-error="handleErrorResponse"></iron-ajax>

this.data =  {
    get: function(sort, page, pageSize) {
            return this.$.listItems.generateRequest().completes;
        }
    }
};

灵感来自https://stackoverflow.com/a/37995462/2795627上更复杂的示例。感谢@akivajgordon。

于 2017-04-05T13:28:17.357 回答