Outward Deviation Pending Promises In Ember

Summary:

We are faced with the following situation with our Ember application and are trying to find a suitable solution.

We are dealing with a lot of data, and therefore some of our data requests are rather slow.

Initially, we used Em.RSVP.hash to β€œbind” our queries in the hook of each route model. However, this blocks the user interface and is ultimately unacceptable.

Our solution was to cascade requests in hookController like this:

setupController: (controller, model)->
    slowRequest1WhichReturnsAPromise().then (data)->
        # Do something with request 1 data

        slowRequest2WhichReturnsAPromise().then (data)->
            # Do something with request 2 data

            slowRequest3WhichReturnsAPromise().then (data)->
                # Do something with request 3 data

It works. We get instant page loading and can display the most relevant data first.

We try to constantly update Ember (1.11.3 at the time of writing). We are not using Ember data.

Problem:

, . , , ( ). , csrf, . , , 422.

- . ( ) . .

, "" .

:

allPromises: []

setupController: (controller, model)->
    deferredRequest1 = new Em.RSVP.defer()

    # Store all promises
    @get('allPromises').pushObject(deferredRequest1)

    # Fire off request 1
    # Spark up the next stage when request 1 finishes
    deferredRequest1.promise.then ->
        deferredRequest2 = new Em.RSVP.defer()
        @get('allPromises').pushObject(deferredRequest2)

# ... 
actions:
    signOut: ->
        @get('allPromises').forEach (promise)->
            promise.reject('canceled')

, .

Ember Docs , " RSVP.Promise ()". , , , .

:

promises/?
, ?

. , , , , .

.

+4
1

, "" signOut. promises, . ( signOut , cookie.)

, signOut , , .

didSignOut: false

setupController: (controller, model) ->
    slowRequest1WhichReturnsAPromise().then ->
        return null if didSignOut
        slowRequest2WhichReturnsAPromise().then ->
            return null if didSignOut
            slowRequest3WhichReturnsAPromise().then ->
                return null if didSignOut
                // ...

:       signOut: β†’           @set ('didSignOut', true)

return null , . , .then() RSVP.on("error", ...).

0

All Articles