Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormData append not working for Objects nor Arrays (recursive). #14

Open
esbanarango opened this issue Apr 28, 2015 · 3 comments · Fixed by #16
Open

FormData append not working for Objects nor Arrays (recursive). #14

esbanarango opened this issue Apr 28, 2015 · 3 comments · Fixed by #16
Assignees

Comments

@esbanarango
Copy link
Collaborator

Let's say I have an ember model that after being serialized looks like this:

{
    company_id: "9", 
    enable_custom_message: false,
    enable_header: false,
    enable_team_members: false,
    product_attributes: {
        description: null,
        enabled: true,
        internal_number: "ECARD3",
        name: "Frosted Lantern"
    }
    wistia_default_video_url: undefined,
    wistia_personalized_video_url: undefined,
}

With the current way of appending data to the formData the product_attributes attribute will have a value of "[object object]". Same happens if I have an array with more than 1 dimension.

formData = new FormData();
root = this._rootKey();
Ember.keys(data).forEach(function(key) {
  if (!Ember.isEmpty(data[key])) {
    // THIS NEEDS TO BE A RECURSIVE FUNCTION
    if (Ember.isArray(data[key])) {
      // ALTHOUGH RAILS DOESN'T SUPPORT MULTI-DIMENSIONAL ARRAYS
      // WE SHOULD SUPPORT IT
      return data[key].forEach(function(val) {
        return formData.append("" + root + "[" + key + "][]", val);
      });
    // MISSING A CHECK FOR AN OBJECT TYPE
    } else {
      return formData.append("" + root + "[" + key + "]", data[key]);
    }
  }
});
esbanarango added a commit to esbanarango/ember-attachable that referenced this issue Apr 28, 2015
@pavloo
Copy link
Collaborator

pavloo commented Apr 28, 2015

@esbanarango hey. yes, it seems it doesn't work. you can issue a PR to fix this. thanks.

@esbanarango
Copy link
Collaborator Author

@sol1dus Sure! Already have the solution working locally. Writing the tests now...

@etzzzz
Copy link

etzzzz commented Aug 11, 2016

FormData.prototype.appendRecursive = function(data, wrapper) {  
    for(var x in data) {
        if(typeof data[x] == 'object' || data[x].constructor === Array) {
            this.appendRecursive(data[x], wrapper + '[' + x + ']');
        } else {
            this.append(wrapper + '[' + x + ']', data[x]);
        }
    }
}
var dataObject = {
    some_field : 'some_value',
    some_field2 : {
        some_field2_1 : 'some_value2_1',
        some_field2_2 : 'some_value2_2'
    },
    some_field3 : 'some_value3'
}
var formData = new FormData();
formData.appendRecursive(dataObject, wrapper); // root wrapper is optional

// Your should get
some_field = some_value
some_field2[some_field2_1] = some_value2_1
some_field2[some_field2_2] = some_value2_2
some_field3 = some_value3

@esbanarango esbanarango reopened this Aug 11, 2016
@esbanarango esbanarango self-assigned this Aug 11, 2016
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 a pull request may close this issue.

3 participants