Skip to content

Request: please pass the model class to dataTransformer #155

@seankndy

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.

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