ember-cli-form-data icon indicating copy to clipboard operation
ember-cli-form-data copied to clipboard

consider having better handling of array values

Open foxnewsnetwork opened this issue 10 years ago • 1 comments

Currently, array values get serialized as comma-separated strings. This is less than ideal since the only real reason to use FormData over json is to handle files, and servers have a hard time figuring out your binary from strings like "[object Object], [object Object]".

Therefore, please consider changing: https://github.com/funtusov/ember-cli-form-data/blob/master/addon/mixins/form-data-adapter.js#L18

Ember.keys(data[root]).forEach(function(key) {
  if (typeof data[root][key] !== 'undefined') {
    formData.append(root + "[" + key + "]", data[root][key]);
  }
});

to...

Ember.keys(data[root]).forEach(function(key) {
  if (Ember.isArray(data[root][key])) {
    data[root][key].forEach(function(value) { 
      formData.append(root + "[" + key + "][]", value);
    });
  } else if (typeof data[root][key] !== 'undefined') {
    formData.append(root + "[" + key + "]", data[root][key]);
  }
});

If this is something we're interested in doing, please let me know and I will submit a PR after I fail my presentation on June 2nd

foxnewsnetwork avatar Jun 01 '15 18:06 foxnewsnetwork

Thanks for the issue, @foxnewsnetwork. I agree that it's a problem that arrays are not supported, good catch!

The problem I see is that different adapters can implement array serialization in different ways. The best approach could be to let the serializer handle array serialization. Maybe you could investigate using something like DS.Adapter#serialize to handle serialization away?

funtusov avatar Jun 01 '15 21:06 funtusov