0

I have currently migrated my cakePHP application from 1.3 to 2.2.4. It manages tasks with tree behavoir. I use jQuery for the ajax.

When I click on a task, an ajax request is fired and the response (the subtasks) update the content of the task div. That works fine for the first level. But the 2nd level does not have any javascript code beeing generated, so the click leads to a blank page, only showing the plain ajax response (only the subtasks of the 2nd level).

How can I tell cakePHP to generate further Javascript code to update the the div? HTML ID's are beeing generated correctly.

Any help would be great!

function expand in TasksController.php:

function expand($id = NULL, $check = 1) {
/*
    get details of given task ID
*/
// if not expanded yet
if ((!$this->Session->check('Task.expand.'.$id))||($check==0)) {
    // set session variable to "expanded"
    $this->Session->write('Task.expand.'.$id, 1);

    // get task details
    $zahlen = array(1 => 'eins', 2 => 'zwei', 3 => 'drei', 4 => 'vier', 5 => 'fünf');
    $this->set('commentlimit', $zahlen[$this->Session->read('Profile.commentcount')]);
    $this->Task->Behaviors->attach('Containable');
    $this->Task->recursive = -1;
    $this->Task->contain(array('Assignee' => array('fields' => array('id', 'name', 'firstname')), 'Tasktype', 'Comment' => array('fields' => 'text', 'limit' => $this->Session->read('Profile.commentcount'), 'order' => 'lft DESC', 'user_id', 'User' => array('fields' => array('name', 'firstname')))));
    $this->set('task', $this->Task->find('first', array('conditions' => array('Task.id' => $id, 'Task.user_id' => $this->Session->read('UserId')))));
}
// if yet expanded, collapse / unload
else {
    $this->Session->delete('Task.expand.'.$id);
    $this->redirect(array('controller' => 'tasks', 'action' => 'collapse'));
}
    //  pr ($this->Session->read('Task'));

    $this->render('expand', 'ajax'); 

    }

And this is my taskelement, which is beeing requested and filled for each task:

<div class="task <?php echo $thisclass; ?>">
    <?php
    echo $this->Js->link($task['Task']['name'], array( 'controller' => 'tasks', 'action' => 'expand', $task['Task']['id'] ), array( 'update' => '#details'.$task['Task']['id'] ));
    echo " <span>(".$this->requestAction('/tasks/countAllChildren/'.$task['Task']['id'])." Unterpunkte, davon ";
    echo $this->requestAction('/tasks/countDirectChildren/'.$task['Task']['id'])." direkt)</span>";
    echo " <span>(".$this->requestAction('/tasks/countAllDoneChildren/'.$task['Task']['id'].'/'.$task['Task']['lft'].'/'.$task['Task']['rght'])." erledigt)</span>";
    ?>
<div id="details<?php echo $task['Task']['id']; ?>">
    <?php
        echo $this->requestAction('/tasks/is_expanded/'.$task['Task']['id']);
    ?>
</div>
    </div>

The relevant part of expand.ctp look like this:

    <?php
    $children = $this->requestAction('/tasks/getChildren/'.$task['Task']['id']);
    if ($children) {
    ?>
<p><h4>Unteraufgaben:</h4>
<?php
echo $this->Html->link('Neu',"/tasks/add/".$task['Task']['id'], array('class'=>'button'));
foreach ($children as $child) {
    $this->set('task', $child);
// todo: das hier eleganter machen, ohne zusätzliche Abfragen
    $this->set('level', $this->requestAction('/tasks/getDepth/'.$task['Task']['id']));
    echo "<tr>".$this->element('taskmesh')."</tr>";
}
    }
    else {
?>
<p><h4>Keine Unteraufgaben</h4>
<?php
echo $this->Html->link('Neu',"/tasks/add/".$task['Task']['id'], array('class'=>'button'));
    }
?>
</p>
4

1 回答 1

0

When I asked another CakePHP guy, he told me to use the JavaScript buffer and linked to it in the CakeBook: http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html

So the answer to my question is:

Add the following code the bottom of the view of the requested ajax action:

echo $this->Js->writeBuffer(); // Write cached scripts
于 2013-01-16T22:06:43.310 回答