From 7090bb15c3030e05c9c311d8de69c01ca9abfa6e Mon Sep 17 00:00:00 2001 From: Salvador de la Puente Date: Sun, 26 Apr 2015 20:46:25 +0200 Subject: [PATCH 1/2] Improving customization --- addon/mixins/form-data-adapter.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/addon/mixins/form-data-adapter.js b/addon/mixins/form-data-adapter.js index e14028a..dcfb64e 100644 --- a/addon/mixins/form-data-adapter.js +++ b/addon/mixins/form-data-adapter.js @@ -13,13 +13,16 @@ export default Ember.Mixin.create({ if (typeof FormData !== 'undefined' && data && this.formDataTypes.contains(type)) { var formData = new FormData(); - var root = Ember.keys(data)[0]; - - Ember.keys(data[root]).forEach(function(key) { - if (typeof data[root][key] !== 'undefined') { - formData.append(root + "[" + key + "]", data[root][key]); + var fields = this.getDataToTransform(data); + + Ember.keys(fields).forEach(function(key) { + if (typeof fields[key] !== 'undefined') { + formData.append( + this.getFormKey(key, fields), + this.getFormValue(key, fields) + ); } - }); + }.bind(this)); hash.processData = false; hash.contentType = false; @@ -28,4 +31,19 @@ export default Ember.Mixin.create({ return hash; }, + + getDataToTransform: function (data) { + var root = Ember.keys(data)[0]; + return data[root]; + }, + + getFormKey: function (key, data) { + this._root = this._root || Ember.keys(data)[0]; + return this._root + "[" + key + "]"; + }, + + getFormValue: function (key, data) { + this._root = this._root || Ember.keys(data)[0]; + return data[this._root][key]; + } }); From a2e9c342ff8539fc6f746d072e9e1eb743dd2f04 Mon Sep 17 00:00:00 2001 From: Salvador de la Puente Date: Fri, 1 May 2015 10:29:58 +0200 Subject: [PATCH 2/2] Adding documentation and improving API naming. --- README.md | 8 +++++++- addon/mixins/form-data-adapter.js | 20 +++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f2d420c..1d38dc4 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,12 @@ model.save(); This will send the ``attachment`` and all other attributes as a FormData object. +## Customization + +You can override `getFormFields()` to select the fields you want to transform into FormData. The method is passed the model snapshot as unique parameter. If you want to customize the name for the key, overrides `getFormKey()` which is passed with the `key` and `value` for that key of the hash returned by `getFormFields()`. To customize the value, use `getFormValue()`. + +Do not abuse these customization. If you're performing huge changes in your model's representation, then you should be providing a custom serializer or maybe a different transformation for an specific field. + ### Thanks -This addon was inspired by Matt Beedle's blog post http://blog.mattbeedle.name/posts/file-uploads-in-ember-data/ \ No newline at end of file +This addon was inspired by Matt Beedle's blog post http://blog.mattbeedle.name/posts/file-uploads-in-ember-data/ diff --git a/addon/mixins/form-data-adapter.js b/addon/mixins/form-data-adapter.js index dcfb64e..8c4f3e9 100644 --- a/addon/mixins/form-data-adapter.js +++ b/addon/mixins/form-data-adapter.js @@ -13,13 +13,13 @@ export default Ember.Mixin.create({ if (typeof FormData !== 'undefined' && data && this.formDataTypes.contains(type)) { var formData = new FormData(); - var fields = this.getDataToTransform(data); + var fields = this.getFormFields(data); Ember.keys(fields).forEach(function(key) { if (typeof fields[key] !== 'undefined') { formData.append( - this.getFormKey(key, fields), - this.getFormValue(key, fields) + this.getFormKey(key, fields[key]), + this.getFormValue(key, fields[key]) ); } }.bind(this)); @@ -32,18 +32,16 @@ export default Ember.Mixin.create({ return hash; }, - getDataToTransform: function (data) { - var root = Ember.keys(data)[0]; - return data[root]; + getFormFields: function (data) { + this._root = this._root || Ember.keys(data)[0]; + return data[this._root]; }, - getFormKey: function (key, data) { - this._root = this._root || Ember.keys(data)[0]; + getFormKey: function (key, value) { return this._root + "[" + key + "]"; }, - getFormValue: function (key, data) { - this._root = this._root || Ember.keys(data)[0]; - return data[this._root][key]; + getFormValue: function (key, value) { + return value; } });