FAQ
Hello everybody,

I am experimenting with Catalyst::Controller::REST and jquery. The
goal is to create a Controller that handles media resources (= upload
a file + text input).

Catalyst::Controller::Resources pretty much does what I want.

Problem: Since I cannot seem to upload a file with a XMLHttpRequest my
Controller needs to handle two kinds of requests
1) typical AJAX requests where only a part of the site is changed.
thus no full document is sent "<html><body>...</body></html>" but only
html fragments or JSON encoded data.
2) entire documents to respond to multipart/form-data POST/PUT requests



Here is a list of actions/paths I want to work with:

GET /media
get a list of all resources (respond with json data)

GET /media/$id
get a resource (respond with json data)

DELETE /media/$id
delete a resource (respond with a status msg, e.g. http status code)

GET /media/new
send form for creating a new resource (<form>...</form>)

POST /media
submit a form, unlike the other requests so far this one expects a
complete document in return. the entire page is reloaded.

GET /media/$id/edit
send form for editing a resource (<form>...</form>)

PUT /media/$id
submit the edit form, unlike. this one expects a complete document in
return like the POST request before



So how do I handle the two kinds of output?
One time I want to respond with html documents generated by my
C::V::TT View (POST, PUT requests) and for ajax requests I'd like to
use the serialization mechanism of C::C::REST.
I accomplished this by setting

MyApp/Controller/Media.pm:
__PACKAGE__->config(
map => {
'text/html' => [ 'View', 'MyView' ], # lib/MyApp/View/MyView.pm
}
);

Otherwise catalyst returns a 415 "Content-Type text/html is not supported" msg.
Now form POST/PUT requests are answered with completely new html pages
while ajax requests with jquery dataType "json" will get the
application/json response provided by C::C::REST


I am asking myself if my approach is practicable.
Any input welcome.

david

ps: I want my websites to be able to work with and without js. How can
I achieve this without increasing the complexity of my code too much.
(I vaguely remember this was really easy in RoR)

Search Discussions

  • Moritz Onken at May 18, 2010 at 11:47 am

    Am 18.05.2010 um 17:47 schrieb David Schmidt:

    ps: I want my websites to be able to work with and without js. How can
    I achieve this without increasing the complexity of my code too much.
    (I vaguely remember this was really easy in RoR)
    Your approach seems perfectly reasonable. To make your REST interface
    work with browser have a look at
    http://search.cpan.org/perldoc?Catalyst::TraitFor::Request::REST::ForBrowsers

    Simply add a hidden form element with the name x-tunneled-method and the
    http method as value to tunnel the request as the desired http method.


    cheers,
    mo

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedMay 18, '10 at 9:47a
activeMay 18, '10 at 11:47a
posts2
users2
websitecatalystframework.org
irc#catalyst

2 users in discussion

Moritz Onken: 1 post David Schmidt: 1 post

People

Translate

site design / logo © 2021 Grokbase