Add the ability to return non-nested responses

This functionality means we no longer need to use JavaScript on the client
side to flatten the data.

The code has been written so it should be relatively straightforward to strip
out the grouped/nested code in the future when Backdrop changes to only return
flattened responses.
Switching on the flatten parameter four times in this function seems a bit over the top. I think that a more structural refactoring would make more sense so that there can be just one switch.