[Catalyst] DELETE through REST and Jemplate

J. Shirley jshirley at gmail.com
Mon Mar 30 18:46:57 BST 2009


On Mon, Mar 30, 2009 at 10:31 AM, Emmanuel Quevillon <tuco at pasteur.fr>wrote:

> J. Shirley wrote:
>
>> On Mon, Mar 30, 2009 at 7:39 AM, Emmanuel Quevillon <tuco at pasteur.fr<mai=
lto:
>> tuco at pasteur.fr>> wrote:
>>
>>    J. Shirley wrote:
>>    > On Mon, Mar 30, 2009 at 7:24 AM, Emmanuel Quevillon
>>    <tuco at pasteur.fr <mailto:tuco at pasteur.fr>
>>    > <mailto:tuco at pasteur.fr <mailto:tuco at pasteur.fr>>> wrote:
>>    >
>>    >     Hi,
>>    >
>>    >     I am playing with REST and Jemplate from the great Tuts from
>>    JRock's
>>    >     book.
>>    >     However, I am facing a problem. I understand well how to
>>    retrieve
>>    >     data from a REST url and playing Ajax with it, by a GET method.
>>    >     However, I don't know how to change the method to DELETE from a
>>    >     simple link like:
>>    >
>>    >     test.js contains:
>>    >
>>    >     function remove_item(id){
>>    >      Jemplate.process('test.tt2',
>>    >                      '[% Catalyst.uri_for("/rest/item/") %]'+id,
>>    >                      '#_item');
>>    >     }
>>    >
>>    >     template test.tt2:
>>    >
>>    >     <a href=3D"#" onclick=3D"remove_item('[% item.id
>>    <http://item.id> <http://item.id> %]');
>>
>>    >     return
>>    >     false;">Remove item</a>
>>    >
>>    >     If I code my template like this, it just do a GET request.
>>    >     How can I introduce DELETE method  to tell REST API to delete my
>>    >     item id?
>>    >
>>    >     Sorry if my question seems stupid, I am a bit lost.
>>    >     If anybody has any clue, very welcome.
>>    >
>>    >     Regards
>>    >     --
>>    >     -------------------------
>>    >     Emmanuel Quevillon
>>    >     Biological Software and Databases Group
>>    >     Institut Pasteur
>>    >     +33 1 44 38 95 98
>>    >     tuco at_ pasteur dot fr
>>    >     -------------------------
>>    >
>>    >
>>    > You didn't include your remove_item method.  Can you post that?
>>    >
>>    > -J
>>
>>    It is very basic, it took code from Jrock's book :)
>>
>>    sub item_DELETE {
>>
>>       my($self, $c) =3D @_;
>>
>>       my $id =3D $c->stash()->{id};
>>
>>       my $set =3D $c->model('MyAppDB::Item')->find({id =3D> $id});
>>
>>       if($set){
>>           $self->status_ok($c,
>>                            entity =3D> mk_entity($set));
>>           $set->delete();
>>       }
>>       else{
>>           $self->status_not_found($c,
>>                                   message =3D> "Item not found");
>>       }
>>    }
>>
>>    --
>>
>>
>> I meant the javascript method, not the Catalyst method.
>>
>> There should be a "function remove_item()" somewhere, as it doesn't seem
>> part of Jemplate.  Also, you probably want to use jQuery or something mo=
re
>> robust for your asynchronous calls.  Jemplate docs don't even seem to ha=
ve a
>> delete method.
>>
>> -J
>>
> Ooops sorry, but it is already in the first email, mentionned as 'in
> test.js':
>
> function remove_item(id){
>     Jemplate.process('test.tt2',
>                     '[% Catalyst.uri_for("/rest/item/") %]'+id,
>                     '#_item');
>    }
>
> Thanks for this help
>
> Emmanuel
>

Oh, I missed it because it just is a Jemplate.process line... didn't even
see the remove_item.

That's a GET request, since it just fetches something remotely from the
server and does nothing else.

You need to change the method to DELETE, and then handle the response.
You'll have to read the pod on Jemplate and jQuery and figure out exactly
how to change the request method to DELETE, and then process the response.
The Jemplate pod has a bit that shows how to use jQuery directly to get the
response (using getJSON) -- you should just have to modify that bit
accordingly.

As part of the jquery docs on the ajax object, it seems just setting "type"
to "DELETE" should do the trick.  You can read more here:

http://docs.jquery.com/Ajax/jQuery.ajax#options


-J
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20090330/ff2e9=
87e/attachment.htm


More information about the Catalyst mailing list