On Thu, 2011-01-20 at 03:03 -0800, Ratty wrote:
> I am trying to use JQuery with CakePHP (1.3.6) using the JS helper.
> 
> I have a bit of code that works fine with the prototype library but
> when I change the library to JQuery, it breaks. The correct function
> in the controller class is definately called and the database update
> in the function happens. But when the function calls $this-
> >render('add_success','ajax'); at the end to render 'add_success.ctp'
> using the 'ajax.ctp' layout... nothing happens.
> 
> Is there something I need to do to make JQuery work with the JS
> library other than those detailed in the 1.3 Cookbook ?
> 
> I hope someone can help or at least point me in the right direction.
> 

In case it helps...  Here are some code snippets of what I am trying to
do.

app_controller.php calls registers the Js Helper accross the entire
application ( I will localise this when I get it working ). To switch to
JQuery I use 'Js'=>array('JQuery')

class AppController extends Controller {
    ...
    var $helpers = array( 'Form', 'Html', 'Js' => array('Prototype'));
    ...
}



default.ctp layout includes the javascript library and writes any
buffered output just before </body>. Again, I change the 'prototype' to
'jquery'  ( I am using jquery-1.4.4 )

<head>
        <title><?php echo $title_for_layout; ?></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <?php echo $html->css( 'global' ); ?>
        <?php echo $this->Html->script('prototype');?>
</head>
<body>
<div id="container">
  <div id="content">
    <div id="content_left">
        ...
    </div>
    <!-- end of content left -->
    <div id="content_right">
        <?php echo $content_for_layout ?>
    </div>    
    <!-- end of content right -->
  </div>
  <!-- end of content -->
  <div id="footer"> 
    ...
  </div>
  <!-- end of footer -->
</div>
<!-- end of container -->
<?php echo $this->element('sql_dump'); ?>
<?php echo $this->Js->writeBuffer(); ?>
</body>
</html>


My comments controller allows comments to be added to a post. Each
comment can also be voted on.

class CommentsController extends AppController {
...
  function add() { 
    if(!empty($this->data)){ 
      $this->Comment->create();
      if($this->Comment->save($this->data)) { 
        $comments=$this->Comment->find('all', array(
          'conditions'=>array(
            'post_id'=>$this->data['Comment']['post_id']), 
            'recursive'=>-1)); 
        $this->set(compact('comments')); 
        $this->render('add_success','ajax'); 
      }else{ 
        $this->render('add_failure','ajax'); 
      } 
    } 
  }
        
  function vote($type=null,$id=null) { 
    if($id){ 
      $votes=$this->Comment->vote($type,$id); 
      $this->set(compact('votes')); 
      $this->render('votes','ajax'); 
    } 
  }

   ...
}

add_success.ctp displays the comment just added.
add_failure.ctp displays an error message
votes.ctp displays the number of votes for the comment

The posts view displays a post and allows comments to be added and voted
on through Js

<h1><?php echo $post['Post']['name'];?></h1> 
<hr/> 
<?php echo $bbcode->parse($post['Post']['content']); ?>
<hr/>
<h2>Comments</h2>
<div id="comments">
<?php foreach($comments as $comment): ?> 
  <div class="comment">
    <div id="vote_<?php echo $comment['Comment']['id'];?>"> 
      <div class="cast_vote"> 
        <ul> 
          <li><?php echo $this->Js->link('up', array(
                         'controller'=>'comments', 
                         'action'=>'vote', 'up',
                         $comment['Comment']['id']),
                         array('update'=>'vote_'.
                               $comment['Comment']['id']));
             ?>
          </li> 
          <li><?php echo $this->Js->link('down',array(
                         'controller'=>'comments', 
                         'action'=>'vote', 'down',
                         $comment['Comment']['id']),
                         array('update'=>'vote_'.
                               $comment['Comment']['id']));
              ?>
          </li> 
        </ul> 
      </div> 
      <div class="vote">
        <?php echo $comment['Comment']['votes'];?>
      </div> 
    </div>
    <p><b><?php echo $comment['Comment']['name'];?></b></p> 
    <p><?php echo $comment['Comment']['content'];?></p> 
  </div> 
<?php endforeach;?>
<?php echo $form->create('Comment'); ?>
<?php echo $form->input('name', array('label'=>'Enter your name'));?> 
<?php echo $form->input('content', array('label'=>'Enter your
comment'));?> 
<?php echo
$form->input('post_id',array('type'=>'hidden','value'=>$post['Post']['id']));?>
<?php echo $js->submit('Add Comment', array('url' =>
array('controller'=>'comments', 'action' => 'add' ),
'update'=>'comments')); ?>
<?php echo $form->end();?>
</div>

Nothing startling here. With prototype, the vote count goes up and down
when the link is selected and added comments are displayed... but not
with JQuery.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to