Class PromiseProxyMixin
publicimport PromiseProxyMixin from '@ember/object/promise-proxy-mixin'; |
A low level mixin making ObjectProxy promise-aware.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import { resolve } from 'rsvp'; import $ from 'jquery'; import ObjectProxy from '@ember/object/proxy'; import PromiseProxyMixin from '@ember/object/promise-proxy-mixin'; let ObjectPromiseProxy = ObjectProxy.extend(PromiseProxyMixin); let proxy = ObjectPromiseProxy.create({ promise: resolve($.getJSON('/some/remote/data.json')) }); proxy.then(function(json){ // the json }, function(reason) { // the reason why you have no json }); |
the proxy has bindable attributes which track the promises life cycle
1 2 3 4 |
proxy.get('isPending') //=> true proxy.get('isSettled') //=> false proxy.get('isRejected') //=> false proxy.get('isFulfilled') //=> false |
When the $.getJSON completes, and the promise is fulfilled
with json, the life cycle attributes will update accordingly.
Note that $.getJSON doesn't return an ECMA specified promise,
it is useful to wrap this with an RSVP.resolve
so that it behaves
as a spec compliant promise.
1 2 3 4 |
proxy.get('isPending') //=> false proxy.get('isSettled') //=> true proxy.get('isRejected') //=> false proxy.get('isFulfilled') //=> true |
As the proxy is an ObjectProxy, and the json now its content, all the json properties will be available directly from the proxy.
1 2 3 4 5 6 7 8 9 |
// Assuming the following json: { firstName: 'Stefan', lastName: 'Penner' } // both properties will accessible on the proxy proxy.get('firstName') //=> 'Stefan' proxy.get('lastName') //=> 'Penner' |
catch (callback) RSVP.Promise public
Defined in packages/@ember/-internals/runtime/lib/mixins/promise_proxy.js:192
Available since v1.3.0
- callback
- Function
- returns
- RSVP.Promise
An alias to the proxied promise's catch
.
See RSVP.Promise.catch.
finally (callback) RSVP.Promise public
Defined in packages/@ember/-internals/runtime/lib/mixins/promise_proxy.js:205
Available since v1.3.0
- callback
- Function
- returns
- RSVP.Promise
An alias to the proxied promise's finally
.
See RSVP.Promise.finally.
then (callback) RSVP.Promise public
Defined in packages/@ember/-internals/runtime/lib/mixins/promise_proxy.js:180
- callback
- Function
- returns
- RSVP.Promise
An alias to the proxied promise's then
.
See RSVP.Promise.then.