Skip to content

Conversation

@delapuente
Copy link

In a project I'm working on, we choose to not wrap server responses. FormData Adapter seems pretty good but it assumes a rooted serialization. This alternative implementation does the same but allows the developer for further customization of what to be serialized into form data and how this serialization must to be done. What do you think?

Thanks for the plugin anyway!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please clarify, why do we need both the iteration over fields and passing the whole fields array to those functions from within the iterations? I.e. couldn't we do something like this.getFormKey(key, fields[key])

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in .forEach() I would pass the collection you're iterating over, you could be depending of relationships among the whole data being serialized. Anyway, as I can not imagine a clear use case for this right now, I agree on passing only key, fields[key].

@funtusov
Copy link
Owner

@lodr Thanks for the pull request! I like the idea of allowing customization, provided that the default isn't changed.

Could you please give an example of how this customization would be used and add/fix the tests?

@delapuente
Copy link
Author

Of course. Here is the example based on my own code:

import ApplicationAdapter from './application-adapter';
import FormDataAdapterMixin from 'ember-cli-form-data/mixins/form-data-adapter';

var OriginAdapter = ApplicationAdapter.extend(FormDataAdapterMixin, {
  getDataToTransform(data) {
    return data;
  },

  getFormKey(key, data) {
    return key;
  },

  getFormValue(key, data) {
    return data[key];
  }
});

export default OriginAdapter;

As you can see, I avoid the JSON root so I'm returning the same data object and so, for the serialization I'm skipping the hash[key], value format to simply use key, value.

What do you think?

@delapuente
Copy link
Author

@funtusov Tests in PhantomJS are failing on my side, not in this branch but in master as well. In Firefox they run smooth and the introduced changes did not break anything. Anyway, do you want me to add some tests for the customization capabilities?

@funtusov
Copy link
Owner

@lodr I'm sorry for the late reply! I like this customisation approach, if you're still interested, could you please fix the current tests for phantomjs and add tests for those 3 new methods.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants