JAX - jQuery Ajax Helper for CakePHP
Olá pessoal,
intediado com o helper do cake que trabalha com prototype? Eu também...
Então resolvi criar (inicialmente para uso próprio) o JAX - jQuery Ajax Helper. Que funciona de forma bem semelhante ao helper nativo do cake, mas com algumas melhorias...
O código pode ser visto abaixo: (e baixado aqui)
PHP:
-
/*
-
JAXHelper - CakePHP Helper for Ajax with jQuery
-
Author: Tulio Faria (www.tuliofaria.net)
-
Requeriments:
-
- jquery: http://docs.jquery.com/Downloading_jQuery
-
- jform (for some features like observeForm): http://jquery.com/plugins/project/form
-
Using:
-
- include jquery.js and jquery.form.js in your view (yes, using plain html or html helper
) -
- include the helper Jax in your controller
-
- call any of methods available: link, observeField and observeForm
-
*/
-
-
class JaxHelper extends Helper {
-
-
-
$url = $this->Html->url($url);
-
$att = " ";
-
-
-
foreach($options as $k=>$v){
-
$att.=$k.'="'.$v.'" ';
-
}
-
-
}
-
-
\$(\"$target\").html(data); }); return false; }); ");
-
}
-
function observeField($element, $options){
-
$event = $options["event"];
-
$update = $options["update"];
-
$url = $this->Html->url($options["url"]);
-
-
$code = $options["loading"].'
-
$.ajax({
-
type: "POST",
-
url: "'.$url.'",
-
data: $("'.$element.'").serialize(),
-
success: function(data){
-
$("'.$update.'").html(data);
-
'.$options["complete"].'
-
}
-
});
-
';
-
-
return $this->_jsBlock($this->_addReady("\$(\"$element\").$event(function(){ $code })"));
-
}
-
-
function observeForm($element, $options){
-
$event = $options["event"];
-
$update = $options["update"];
-
$url = $this->Html->url($options["url"]);
-
-
$code = '$("'.$element.'").ajaxSubmit({
-
target: \''.$update.'\',
-
beforeSubmit: function(){'.$options["loading"].'},
-
success: function(){'.$options["complete"].'}
-
}
-
);';
-
-
return $this->_jsBlock($this->_addReady("\$(\"$element\").$event(function(){ $code return false; })"));
-
}
-
-
function _jsBlock($content){
-
return "<script type="\">$content</script>";
-
}
-
function _addReady($content){
-
return "\$(function(){ $content } );";
-
}
-
-
function test(){
-
echo $this->_jsBlock($this->_addReady("alert(\"Jax Helper has been installed and ready to use!\");"));
-
}
-
}
-
?>
Exemplos:
Link:
PHP:
Observe field:
PHP:
-
$options["event"] = "change";
-
$options["update"] = "#teste";
-
$options["url"] = "/contatos/info/1/";
-
$options["loading"] = "$('#loading').fadeIn('slow');$('#teste').hide();";
-
$options["complete"] = "$('#teste').fadeIn('slow');$('#loading').fadeOut();";
ObserveForm:
PHP:
-
$options["event"] = "submit";
-
$options["update"] = "#teste";
-
$options["url"] = "/contatos/info/1/";
-
$options["loading"] = "$('#loading').fadeIn('slow');$('#teste').hide();";
-
$options["complete"] = "$('#teste').fadeIn('slow');$('#loading').fadeOut();";
-
Em breve publicarei exemplos mais decentes de uso.
Qualquer sugestão ou dúvida, comente!
Abraços,

Danilo Miguel said,
Setembro 28, 2007 @ 16:36
Eu sou suspeito em falar, mas ficou show!
Não testei ainda, era pra ter feito isso antes da publicação, mas vc esqueceu de me enviar.. enrolado...rsrsrs
Mas pelo visto está shoe de bola!!! Vou testar, e usar, o mais rápido possÃvel!
Rafael Marin said,
Setembro 29, 2007 @ 22:44
Opa, sou novo por essas bandas.
To me aventurando com CakePHP e acho que achei um site brasileiro.
Muito bom os artigos sobre o framework.
Abraços
John-Henrique said,
Outubro 1, 2007 @ 16:48
Acho que seria melhor colocar um link para fazer o download do helper, aqui deu erro na hora de desmembrar as quebras de linha.
A propósito o que significa isso?
[code]"
Tulio Faria said,
Outubro 2, 2007 @ 16:14
Dimiguel,
valeu pelo elogio...
Rafael,
pretendo em breve trazer mais notÃcias sobre CakePHP... E ajudar ainda mais quem está começando...
Olá John-Henrique,
está disponÃvel para download...
abraços a todos,
Rafael Brugnollo said,
Outubro 4, 2007 @ 01:33
Falae Tulio Blz...
Cara... sabe me indicar um passo-a-passo para iniciante em Ajax + cakephp?!
valeu cara... to tentando fazer funcionar esse seu código... mas não tenho noção de ajax no cake...
VALEU!!
Tulio Faria said,
Outubro 5, 2007 @ 15:22
Opa, jóia...
Então, eu pretendo fazer um desses... Eu ainda não achei nada muito didático por aÃ...
Abraços,
Guto said,
Novembro 7, 2007 @ 10:19
Perfeito para quem já trabalha com o Jquery, valeu!
Bill said,
Novembro 13, 2007 @ 15:39
Achei bem bacana o Helpers, só falta mostrar um exemplo bem "passo a passo" pra quem apanha de ajax e javascript que nem eu..rs
Abraços amigo
Tulio Faria said,
Novembro 17, 2007 @ 15:50
Bill,
os exemplos virão em breve
abraços,
Guto said,
Novembro 26, 2007 @ 21:57
Só um lembrete, quem baixar o código não esqueça de colocar
Marco Souza said,
Novembro 26, 2007 @ 23:15
Blz a todos , estou gostando dos posts e videos do Tulio, se alguem conseguiu utilizar esses help, post o codigo ai. vlw aguardo
Tulio Faria said,
Dezembro 1, 2007 @ 15:00
Olá, eu estou devendo os exemplos ainda... Em breve irei disponibilizar... Fim de ano sempre é uma grande correria.
Abraços a todos,
Mariano Guezuraga said,
Maio 14, 2008 @ 14:47
For portability you should open you code like <?php and not <?.
I'll check this later, but I was expecting something like this. Nice work!
Tulio Faria said,
Maio 17, 2008 @ 22:27
Hi Mariano,
yes, I have to change this in my scripts. I like to open the php escope with < ?
Thanks for your comment!
Alisson S A Nascimento said,
Agosto 8, 2008 @ 10:11
Vou Baixar! Estou com problemas com o AjaxHelper
Jero Ahumada said,
Agosto 12, 2008 @ 14:30
Just what I was looking for.
I will test it right now.
Thanks!