Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

JSON backend behavior

jgarth edited this page Mar 4, 2012 · 2 revisions

Some examples of JSON in-/output as expected/produced by MCResource

Class "Post", request to GET /posts should yield:

[
	{
		"post":
		{
			"title": "a post",
			"content": "some content",
			"rating":
			{
				"aggregate": 2.5, 
				"some_other_method": 3.1
			},
		"created_at": "2012-01-12T17:30:58+01:00",
		"updated_at": "2012-01-12T17:30:58+01:00",
		"published_at": "2012-01-12T17:30:58+01:00",
		"id": 1,
		}
	},
	{
		"post":
		{
			"title": "another post",
			"content": "some more content",
			"rating":
			{
				"aggregate": 1.9, 
				"some_other_method": null
			},
			"created_at": "2012-01-12T17:30:58+01:00",
			"updated_at": "2012-01-12T17:30:58+01:00",
			"published_at": "2012-01-12T17:30:58+01:00",
			"id": 2,
		}
	},
]

JSON for saving is constructed using FormData objects (transparent for rails if you just use the params hash). This is really convenient if you decide to upload files, MCResource does not care. Also, some work is done by the browser instead of its JS engine.

Saving a comment attached to a post via hasMany-Association will create a PUT request to /posts/1/comments/3

------WebKitFormBoundaryAFOJVz3sALr2LK78
Content-Disposition: form-data; name="comment[title]"

A changed title.
------WebKitFormBoundaryBXIxhz5Rl8Qsjwtj
Content-Disposition: form-data; name="comment[content]"

Some changed content.
It's nice to debug with WebKit's inspector
------WebKitFormBoundaryBXIxhz5Rl8Qsjwtj
Content-Disposition: form-data; name="variant[rating][aggregate]"

6.9
------WebKitFormBoundaryBXIxhz5Rl8Qsjwtj--

and will arrive in Rails like this:

Parameters: {"post_id"=>"1", "id"=>"3", "comment"=>{"title"=>"A changed title.", "content"=>"Some changed content.\nIt's nice to debug with WebKit's inspector", "rating" => {"aggregate" => 6.9}}}

Clone this wiki locally