CakePHP 2.1 - JsHelper / AJAX的回调函数

时间:2012-06-21 22:22:14

标签: ajax cakephp callback helper

对于CakePHP 2.x使用JsHelper,对于每个回调函数,可能有多个选择器受到各种影响。例如,我正在使用:

echo $this->Js->submit('thumbs-up-green.jpg', array(
  'id' => 'thumbs-up-green',
  'before' => $this->Js->get('#thumbs-down-red')->effect('fadeOut'),
  'success' => $this->Js->get('#thumbs-down-gray')->effect('fadeIn')
));

假设我想在回调函数以及中对#thumbs-down-grey应用效果(除了我目前拥有的#thumbs-down-red效果)那是什么语法?我一直在寻找,但JsHelper的文档是有限的。

另外一个更简单的问题,JsHelper提交按钮/表单似乎执行换行,即使CSS显示:none;活跃。我如何摆脱那次换行?

2 个答案:

答案 0 :(得分:1)

我没有找到一个非常简洁的JsHelper解决方案,我认为我不能链接其他元素和行动。

所以我认为对于更复杂的回调函数,解决方案是执行预加载的函数,如:

'before' => 'someFunction();',

但是如果你只是想在一个地方使用fadeOut多个元素,那么你可以像下面这样写:

'before' => $this->Js->get('#thumbs-down-red, #other-element')->effect('fadeOut'),

答案 1 :(得分:1)

您必须在实际的提交调用之前在变量中设置“before”-action。像这样:

$before = $this->Js->get('#skadetyp_form')->effect('fadeOut', array('buffer' => false));
$before .= '$(\'#notice\').append(\'<div class="notice">Sending..<br/>' . $this->Html->image('load_bar.gif', array('alt' => 'Loading..')) . '</div>\')';
$complete = $this->Js->get('#notice div')->effect('fadeOut', array('buffer' => false));
$complete .= '$(\'#notice\').append(\'<div class="success">Succssfully seny!</div>\')';

echo $this->Js->submit('Send!',
        array(
            'update'=>'#skadetyp_form',
            'complete' => $complete,
            'before' => $before,
            'error' => $error,
            'async' => true,
            'method' => 'post',
            'dataExpression'=>true,
            'data'=> $this->Js->serializeForm(
                array(
                    'isForm' => true,
                    'inline' => true
                )
            )
        )
);