Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a Mootools version of what I wanted. I was actually unaware that the Mootools Hash.toQueryString() almost does this by default. It formats arrays in a way that ASP.NET MVC won't recognize so I still needed to implement my own:</p> <pre><code>var POSTEncoder = { getHash: function(request, prefix, out) { if($type(out) != 'object') { out = {}; } function add(key, value){ out[ key ] = value; }; var validPrefix = $type(prefix) == 'string'; switch($type(request)) { case 'array': if(!validPrefix) { prefix = 'request'; } request.each(function(item, i){ POSTEncoder.getHash(item, prefix + '[' + i + ']', out); }); break; case 'object': if(!validPrefix) { prefix = ''; } new Hash(request).each(function(val, key) { POSTEncoder.getHash(val, (prefix != '' ? prefix + '.' : '') + key, out); }); break; case 'string': case 'number': case 'boolean': case 'date': if(!validPrefix) { prefix = 'request'; } add(prefix, request); break; case false: add(prefix || 'request', ''); } return new Hash(out); }}; </code></pre> <p>POSTEncoder.getHash(request) will flatten out the request into key/value pairs suitable for a form POST. POSTencoder.getHash(request).toQueryString() will turn that into a query string.</p> <p>Example:</p> <pre><code>var test = { bool: true, strng: 'a', numbr: 1, basicArr: ['d','e','f'], basicObj: {x:1, y: 'z'}, objectArr: [{n:1, o:2}, {n:3,o:4}], complex: { one: 1, two: 'two', three: [{x:1},{x:2},{x:3}] }}; var hash = POSTEncoder.getHash(test); console.log(hash); console.log(hash.toQueryString()); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload