Skip to content

Instantly share code, notes, and snippets.

@Asenar
Last active December 12, 2015 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Asenar/4768412 to your computer and use it in GitHub Desktop.
Save Asenar/4768412 to your computer and use it in GitHub Desktop.
[not finished] non-intrusive javascript example with pretty notices
function mainfunc (func){
this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
function debug(e){
if(__d && typeof console != 'undefined'){
console.log(e);
}
}
function error(e){
if(__d && typeof console != 'undefined'){
console.error(e);
}
}
function flash(title,message,cssclass,duration){
$('#message').hide();
$('#message').html('<div class="close"><a class="closemessage"><img alt="x" src="/js/images/close.gif"/></a></div><div class="inner_wrapper"><div class="title">'+title+'</div><div class="text">'+message+'</div></div>');
$('#message').removeClass().addClass(cssclass);
$('#message').fadeIn();
setTimeout(function(){$('#message').fadeOut();},duration);
}
function flashInfo(title,message){flash(title,message,'info',3000);}
function flashError(title,message){flash(title,message,'error',10000);}
$(document).ready(function(){
$("#message").hide();
})
<?php
abstract class Controller{
public $auto_render = true; // ajax call will set this to false
/*
...
*/
/** handle ajax form with custom callback
* @param formClass string jquery selector
* @param targetId string id of where the return[content] should be placed
* @param callback array('ok'=>'js_callback_instruction', 'nok'=>'js_callback_instruction')
*
*/
public function initForm($formClass=".form", $targetId="content", $callback = array())
{
if (is_array($callback))
{
if (isset($callback['ok']))
$callback_ok = ('do_ok = function(){'.$callback['ok'].'}');
else
$callback_ok = ('do_ok = function(){flashInfo("Ok", "");}');
if (isset($callback['nok']))
$callback_nok = ('do_nok = function(){'.$callback['nok'].'}');
else
$callback_nok = ('do_nok = function(){flashInfo("Erreur", "");}');
}
else
{
$callback_ok = '';
$callback_nok = '';
}
$msg_ok = array();
$msg_ok['title']='ok';
$msg_ok['desc']=htmlspecialchars('base mise à jour');
$msg_nok['title']='erreur';
$msg_nok['desc']='echec';
if($this->auto_render){
// this allow me to upload files with ajax :)
$this->scripts[]=Kohana::config("config.js_path")."/jquery/jquery.form.js";
$l_updated=$this->get_lang_strings('l_updated');
// that code will be added in <head>
$this->jsheadcode[]=<<<FIN_SCRIPT
$(document).ready(function(){
var options;
$("$formClass").live("submit",function(e){
e.preventDefault();
var options={
type: "post", // 'get' or 'post', override for form's 'method' attribute
beforeSubmit:function(formData, jqForm, options){
$('#$targetId').prepend('<div class="tmp">Loading...</div>');
return true;
},
success:function(responseText, statusText, xhr, \$form){
try{
// that try/catch will hide the errors if json has syntax error
$.parseJSON(responseText);
json_res = responseText;
}
catch (e){
json_res = false;
}
if (json_res)
{
$callback_nok;
$callback_ok;
{ content = json_res.content;
if (json_res.flash)
{
if (json_res.status == "error")
{
flashError("Error", json_res.msg);
if (typeof do_nok == "function")
do_nok();
// @TODO : else do_nok exists, do eval or something
else // @TODO : temporary
flashError("Error", json_res.msg);
}
else
{
if (typeof do_ok == "function")
do_ok();
// @TODO : else do_ok exists, do eval or something
else // @TODO : temporary
flashInfo("$msg_ok[title]",json_res.msg);
}
}
}
else // if this is not js, just replace content
{
content = responseText;
}
if("$targetId"){
$('#$targetId').html(content).prepend('<div class="tmp">Updated</div>');
setTimeout(function(){ $('.tmp').fadeOut();},2000);
}
return true;
}
};
$.extend(options,{target:"#$targetId"});
$(this).ajaxSubmit(options);
}).ajaxError(function(event, request, setting){
// flashError("oO");
});
});
FIN_SCRIPT;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment