How to know when a transaction has been committed?

Hi all,

Loving ember-data (just made the switch from RESTless), but I can’t seem to figure out how I can know when a transaction has been fully committed.

I expected transaction.commit to return a promise, which it doesn’t.

The best I can think of so far is to commit (which removes records that aren’t dirty from the transaction), then wait for either a didCreate, didUpdate, didDelete, becameError or becameInvalid is fired on all records.

But getting that working is likely to be a huge yak shaving expedition. Is there a more elegant solution?

Thanks in advance!

Here’s my current (horrific) solution:

  waitForTransaction: (transaction) ->
      successEvents = ['didCreate', 'didUpdate', 'didDelete']
      failEvents    = ['becameError', 'becameInvalid']
      events = [successEvents, failEvents].flatten()

      dirtyRecords = transaction.get('records').toArray()

      promises = dirtyRecords.map (r) ->
        promise = Ember.Deferred.create()
        eventHandlersForRecord = events.map (e) ->      
          eventHandler = ->
            console.log "Got event #{e} on #{r}"

            eventHandlersForRecord.each (handler) ->
              r.off e, handler

            result =
              event: e
              record: r

            if e in successEvents
              promise.resolve result
            else
              promise.reject result
              
          r.on e, eventHandler 
        promise

      Ember.RSVP.all promises
1 Like

At the moment, I think you cannot do better. Ember Data is on the way of beeing “promistified”, but I think it’s a quite long way. For me your workaround is not that bad, because you manage to keep it in one code block.