mardi 6 octobre 2015

Wait after ember run loop

I "have" to rewrite some parts of ember internals in order to support nested routes in my app.

First of all, please don't judge me I know I shouldn't mess with private variables...

So far I am overriding the buildUrl method in buildURLMixin to build my own urls in there. I started with the createRecord just as a POC

Here's the code so far.

  buildURL: (modelName, id, snapshot, requestType, query)->
    switch requestType
      when 'findRecord' then @urlForFindRecord(id, modelName, snapshot)
      when 'findAll' then @urlForFindAll(modelName)
      when 'query' then @urlForQuery(query, modelName)
      when 'queryRecord' then@urlForQueryRecord(query, modelName)
      when 'findMany' then @urlForFindMany(id, modelName, snapshot)
      when 'findHasMany' then @urlForFindHasMany(id, modelName)
      when 'findBelongsTo' then @urlForFindBelongsTo(id, modelName)
      when 'createRecord' then (Ember.run.scheduleOnce 'actions', @, ()->
        @urlForCreateRecord(modelName, snapshot)).then (url)->
        console.log 'URL', url
        url
      when 'updateRecord' then @urlForUpdateRecord(id, modelName, snapshot)
      when 'deleteRecord' then @urlForDeleteRecord(id, modelName, snapshot)
      else @_buildURL(modelName, id)


  urlForCreateRecord: (modelName, snapshot)->
    parentName = Object.keys(snapshot._belongsToIds).objectAt(0)
    parentId = snapshot._belongsToIds[parentName]
    Ember.RSVP.Promise.resolve(@_myBuildURL(parentName, parentId, modelName))

Please note that in the createRecord case, I am waiting after the 'actions' queue to finish. This is because the Snapshot's _belongsToIds are loaded lazily and I need to wait for the record to materialize. Look at this and follow the chain, you will eventually end up here

The problem that I have is the the response from buildURL should be a string. right now I suspect it returns a promise.

Is there a way to wait after the action queue in a blocking manner. If not, do you have any suggestions on what method I could override to make sure it has the correct behavior

Aucun commentaire:

Enregistrer un commentaire