Monday, September 28, 2009

Re: Ajax (jquery) Question - am I doing this right?

Hi Brian,

thanks for your answer.

That's a neat trick to avoid the switch statement!


I've done the redirecting, because i thought it's more DRY. With your
solution (which works perfect!) i need to copy the admin_index method
in the admin_switchStatus method.
(In this particular case, it's only a pagination statement...so no
problem)...

But what if the logic comes complexer? Should I then move it to the
model?


On Sep 29, 2:22 am, brian <bally.z...@gmail.com> wrote:
> On Mon, Sep 28, 2009 at 3:27 PM, cronet <cro...@gmx.de> wrote:
>
> > Hey,
>
> > i try to do my first steps with ajax (jquery) - In a list, i would
> > like to have a switch which changes the 'active' paramter in the db
> > for this entry.
>
> > As i understand this topic, I need to do an ajax call to a controller
> > method, which do the changes, and the realod the div with the new
> > entries...
>
> > so this is my controller method:
>
> >        function admin_switchStatus($id) {
> >                $this->News->id = $id;
> >                $news = $this->News->read('active', $id);
>
> >                switch($news['News']['active']) {
> >                        case "1":
> >                                $this->News->saveField('active', 0);
> >                                break;
> >                        case "0":
> >                                $this->News->saveField('active', 1);
> >                                break;
> >                }
>
> >                $this->redirect(array('controller'=>'news', 'action'=>'index',
> > 'admin'=>true));
> >        }
>
> > the jQuery for this in the admin index view is:
>
> > $('.switchStatus').click(function(){
> >        $.get($(this).attr('href'), function(data){
> >                $('#mainContent').html(data);
> >        });
> >        return false;
> > });
>
> > Is this the correct (?) way to achieve this?
>
> > Basically it works - but cake reloads not the admin_index but the
> > index view...
>
> I'm not sure why it's not rendering the admin view but you shouldn't
> be redirecting. Here's a simpler version:
>
> function admin_switchStatus($id)
> {
>         /* this also sets the model ID
>          */
>         $news = $this->News->read(null, $id);
>
>         /* toggle field
>          */
>         $this->News->saveField('active', 1 - $news['News']['active']);
>
>         /* need to have RequestHandler in $components
>          */
>         if ($this->RequestHandler->isAjax())
>         {
>                 Configure::write('debug', 0);
>                 $this->layout = 'ajax';
>         }
>         $this->render('admin_index');
>
> }
>
> $('.switchStatus').click(function()
> {
>         $.ajax({
>                 url: $(this).attr('href'),
>                 type: 'GET',
>                 success:
>                         function(data)
>                         {
>                                 $('#mainContent').html(data);
>                         },
>                 error:
>                         function (XMLHttpRequest, textStatus, errorThrown)
>                         {
>                                 alert(textStatus);
>                         }
>         });
>         return false;
>
>
>
> });
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: