[Catalyst] implementing ajax

Mesdaq, Ali amesdaq at websense.com
Tue Mar 11 23:30:28 GMT 2008


As I said I use mochikit which is very lightweight and makes ajax and
pretty much everything in javascript very easy but doesnt modify things
like other libraries.

This js code is assuming you have two drop down elements with an id of
'drop_make' and another one with 'drop_year'. initMake needs to be
called after those elements on the page.

===============JS===============

function initMake ()
{
    var drop_make = getElement('drop_make');
    var drop_year = getElement('drop_year');
    
    connect
    (
        drop_make,
        'onchange',
        function ()
        {
            make = drop_make.options[drop_make.selectedIndex].text;
            make_id = drop_make.options[drop_make.selectedIndex].value;
            
            var params = new Object;
            params['year'] = year;
            params['make_id'] = make_id;
            
            var url = '/inventory/getModels';
            var d = doXHR
            (
                url,
                {
                    'Accept': 'text/xml',
                    'method': 'POST',
                    "sendContent" : queryString(params),
                    "headers":
                    {
 
"Content-Type":"application/x-www-form-urlencoded"
                    }
                }
            );
            
            d.addCallback
            (
                function (req)
                {
                    var opt = evalJSONRequest(req);
                    //if you have firebug you can basically dump the
object to firebug console
                    console.log(opt.json);
                }
            );
        }
    );
}


===============Catalyst===============

sub getModels : Local
{
    my ( $self, $c ) = @_;
    # my posted params
    my $year = $c->request->params->{year};
    my $make_id = $c->request->params->{make_id};
    
    #modified my code to just give an example
    my $make = $c->model('adminDB::make')->find($make_id);
    my $models = $c->model('adminDB::model')->search
    (
         { make_id => $make_id, year => $year }
    );

    my $json;
    
    #This should work but its untested and modified from my original
code to make it simpler
    foreach my $model (@{$models})
    {
        push(@{$json}, {model_id => $model->model_id, model =>
$model->model});
    }
    
    $c->stash->{json} = $json;
    $c->stash->{current_view} = 'JSON';
} 


Thanks,
------------------------------------------
Ali Mesdaq (CISSP, GIAC-GREM)
Security Researcher II
Websense Security Labs
http://www.WebsenseSecurityLabs.com
------------------------------------------

-----Original Message-----
From: Jennifer Ahn [mailto:jenahn at stanford.edu] 
Sent: Tuesday, March 11, 2008 2:28 PM
To: The elegant MVC web framework
Subject: Re: [Catalyst] implementing ajax

Hi Ali

Thanks for your suggestions.  I was a little reluctant to explor JSON
because I didn't find the cpan documentation or catalyst book being
sufficient. Being that this application is my first with catalyst, I
wanted to tackle one thing at time. Catalyst first, then all the plug in
modules.  If you could provide a working example of hos to implement the
js and catalyst controller part of JSON, I would greatly appreciate it!

Thanks

jennifer

Mesdaq, Ali wrote:
> Jennifer,
>
> Are you sure you want to stick with this route? Looks like your going 
> to make a lot of work for yourself. I would hate to see the js code to

> make xml. Use the js code to just post the values you want read those 
> values in with something like $c->request->params->{whatever} in your 
> controller. Then in your controller just spit out json which is not 
> even really a line of code. If your using the JSON view all you need 
> to do is put your data in the $c->stash->{json} and it will auto 
> serialize it for you.
>
> Why would you want to use JSON? Because javascript can just read it 
> straight in and you can use it without having to parse anything. Your 
> js would just eval the returned json content and use it. If you return

> xml you will need to parse it out and just makes a lot of work.
>
> If you want I can maybe create something on the wiki on how to use 
> mochikit for the js part of it and catalyst for the controller part of

> it. Maybe its a good advent calendar article anyone think it would be 
> good?
>
> Thanks,
> ------------------------------------------
> Ali Mesdaq (CISSP, GIAC-GREM)
> Security Researcher II
> Websense Security Labs
> http://www.WebsenseSecurityLabs.com
> ------------------------------------------
>
> -----Original Message-----
> From: Jennifer Ahn [mailto:jenahn at stanford.edu]
> Sent: Monday, March 10, 2008 9:37 AM
> To: The elegant MVC web framework
> Subject: [Catalyst] implementing ajax
>
> hello!
>
> i'm wondering how one would implement the server side controller end 
> of an application with ajax.  i have the javascript on my client 
> sending an xml file to a uri controller method but, how does the 
> controller process that xml?  is the standard way of doing this by 
> reading in the xml file through stdin?  i'd like to implement this 
> without using JSON of jemplate.
>
> thanks for your help!
>
> jennifer
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>
>  Protected by Websense Messaging Security -- www.websense.com
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: 
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>   

_______________________________________________
List: Catalyst at lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive:
http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/



More information about the Catalyst mailing list