FileAPI doesn't format js-objects correctly #121

Closed
warappa opened this Issue Sep 17, 2013 · 4 comments

Projects

None yet

2 participants

@warappa
warappa commented Sep 17, 2013

I have a js-object like this:

{
    name: "asdf",
    items: [{
        itemName: "a"
    },
    {
        itemName: "b"
    }]
}

In the POST an item is encoded as items[0][itemName]. Asp .NET model binder doesn't like that (is it even standard conform?). What it likes is items[0].itemName.

So I created a patch which fixes the issue.

DISCLAMER: I'm new to git.

As I can't attach the patch, here is it:

diff --git a/lib/FileAPI.core.js b/lib/FileAPI.core.js
index 175076b..2d94568 100644
--- a/lib/FileAPI.core.js
+++ b/lib/FileAPI.core.js
@@ -1047,11 +1047,17 @@

                // Append data
                _each(options.data, function add(val, name){
-                   if( typeof val == 'object' ){
+                   var isArray = Object.prototype.toString.call(val) === "[object Array]";
+                   if( isArray == true ){
                        _each(val, function (v, i){
                            add(v, name+'['+i+']');
                        });
                    }
+                   else if( typeof val == 'object' ){
+                       _each(val, function (v, i){
+                           add(v, name + '.' + i);
+                       });
+                   }
                    else {
                        Form.append(name, val);
                    }
Contributor
RubaXa commented Sep 17, 2013

I understood you.
But it is better to do so:

// .NET
var FileAPI = {
    postNameConcat: function (name, idx){
          if( idx != null ){
               name += (idx >= 0 ?  '['+ idx +']' : '.' + idx);
          }
          return name;
    }
};


// Default
var FileAPI = {
    postNameConcat: function (name, idx){
          if( idx != null ){
               name += '['+ idx +']';
          }
          return name;
    }
};
warappa commented Sep 17, 2013

Did you mean this should be already supported? If yes, which version is required because I'm using 1.2.4 and tried 1.2.6 but both don't pick up postNameConcat.

Contributor
RubaXa commented Sep 17, 2013

Alas, no, but in the evening certainly will do.

@RubaXa RubaXa added a commit that referenced this issue Sep 17, 2013
@RubaXa RubaXa #121: + FileAPI.postNameConcat 146c044
Contributor
RubaXa commented Sep 17, 2013

Done.

@RubaXa RubaXa closed this Sep 17, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment