I would like to use tg2.0 purely as RESTful JSON middleware along with
jquery 1.4.2. However, I must be misunderstanding how JSON is supposed
to be passed via PUT for modifying the objects. In this case, I
created a "Person" model with a name and a one-to-many relationship to
emails (person can have multiple emails).

When I GET from /person/1.json, the HTTP body is:
{"model": "Person", "value": {"title": "Captain", "surname": "Doe",
"emails": [1, 2], "firstname": "John", "id": 1}}

but when PUT'ing back to the same URL:
{"id
":"1
","firstname
":"John","surname":"Doh","title":"Captain","sprox_id":"","emails":
[1,2]}

with this:
               data=JSON.stringify(data);
               $.ajax({
               url:ajaxURL,
               type:'PUT',
               data:data,
               dataType:'json',
               contentType:'application/json',
               success:function(data,status,request)
               {
               },
               error:ajaxFailed
               });

it seems that the CrudRestController really wants to see the url-
encoded form for the put method (which is obviously not JSON),
otherwise the **kw in the put method does not get populated.

If I use the form url encoded stuff this way:

               //data=JSON.stringify(data);
               $.ajax({
               url:ajaxURL,
               type:'PUT',
               data:data,
               //dataType:'json',
               //contentType:'text/javascript',
               success:function(data,status,request)
               {
               },
               error:ajaxFailed
               });
           });
where "data" is a javascript object, to generate:
id=1&firstname=John&surname=Doh&title=Captain&sprox_id=&emails%5B
%5D=1&emails%5B%5D=2

the put method gets "emails[]":[1,2] (note the extra brackets) in the
arguments which prompts the sprox saormprovider to delete the related
emails (apparently because there is no "emails" key).

I thought one of these uses would be the default behavior, but I have
hit roadblocks with both approaches. What are the best practices for
tg2 read-write RESTful interfaces?

Welcome to the wonderful world of REST, who's proponents are rather smug about it's alleged superiority, but lot is left to the actual implementation to define - creating headaches as the one you perceive.

SCNR.

Now for some actual help: you need to follow the formencode encoding for form-data. I guess jquery defaults to encoding data the rails/ active-record-way.

Formencode serializes multiple values to something like

  emails-1=1&emails-2=2

I suggest you try and write a serialization function that does that to create the proper formencoded string. Then pass that to the AJAX-call.

Diez

--
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en.

Reply via email to