Skip to content

Request: please pass the model class to dataTransformer #155

Description

@seankndy

It would be nice if this plugin would pass the model class as the second argument to the dataTransformer callback so that the dataTransformer function could mutate extra state in the model. Example:

In my model:

export default class FooBar extends Model {
  // ....

  static state() {
    return {
      abilities: null,
    }
  }
}

Now in the transformer:

  dataTransformer: (response, model) => {
    if ('abilities' in response.data) {
      model.commit((state) => {
        state.abilities = response.data.abilities
      })
    }
    return 'data' in response.data ? response.data.data : response.data
  }

I can accomplish this same sort of thing by passing a 'model' into the axios config during the request:

API call:

    const response = FooBar.api()
      .get('/foobars', {
        model: FooBar,
      })

Then in dataTransformer, use it in response.config:

  dataTransformer: (response) => {
    if ('model' in response.config && 'abilities' in response.data) {
      response.config.model.commit((state) => state.abilities = response.data.abilities)
    }

    return 'data' in response.data ? response.data.data : response.data
  }

However it seems reasonable to me that the plugin would pass in the model and make this cleaner.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions