Skip to content

Instantly share code, notes, and snippets.

@juzna
Created April 5, 2011 02:42
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 juzna/902930 to your computer and use it in GitHub Desktop.
Save juzna/902930 to your computer and use it in GitHub Desktop.
Allowing errors
<?php
class BasePresenter extends \Nette\Presenter {
/**
* @overload When sending AJAX response, send also number error details
* @return array
*/
public function sendPayload() {
// Add errors to payload
if(\Nette\Debug::$errors) {
ob_start();
$data = \Nette\Debug::$errors;
include NETTE_DIR . '/Diagnostics/templates/bar.errors.panel.phtml';
$content = ob_get_clean();
// Prepare payload
$this->getPayload()->errors = array(
'count' => sizeof($data),
'html' => $content,
'list' => array_map(function($item, $cnt) {
list($message, $file, $line) = explode('|', $item);
return get_defined_vars();
}, array_keys($data), array_values($data)),
);
// Clear errors
\Nette\Debug::$errors = array();
}
// Call classic sending
parent::sendPayload();
}
}
/**
* AJAX Nette Framework plugin for jQuery
*
* @copyright Copyright (c) 2011 Jan Dolecek
* @copyright Copyright (c) 2009, 2010 Jan Marek
* @copyright Copyright (c) 2009, 2010 David Grudl
* @license MIT
* @link http://nette.org/cs/extras/jquery-ajax
*/
/*
if (typeof jQuery != 'function') {
alert('jQuery was not loaded');
}
*/
(function($) {
$.nette = {
success: function(payload)
{
// redirect
if (payload.redirect) {
window.location.href = payload.redirect;
return;
}
// state
if (payload.state) {
$.nette.state = payload.state;
}
// snippets
if (payload.snippets) {
for (var i in payload.snippets) {
$.nette.updateSnippet(i, payload.snippets[i]);
}
}
// errors
if (payload.errors) {
$('#nette-debug-panel-errors:first').html(payload.errors.html);
$('#nette-debug-bar a[rel=errors] span').html(payload.errors.count + ' new errors');
$('#nette-debug-bar a[rel=errors]').effect('shake', 100);
}
},
error: function(xhr) {
if(xhr.status >= 500 && xhr.status <= 599) {
var errWin = window.open('', 'error');
errWin.document.write(xhr.responseText);
}
},
updateSnippet: function(id, html)
{
$('#' + id).html(html);
},
// create animated spinner
createSpinner: function(id)
{
return this.spinner = $('<div></div>').attr('id', id ? id : 'ajax-spinner').ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide().css({
position: 'fixed',
left: '50%',
top: '50%'
});
}).appendTo('body').hide();
},
// current page state
state: null,
// spinner element
spinner: null
};
})(jQuery);
jQuery(function($) {
$.nette.createSpinner();
// apply AJAX unobtrusive way
$('a.ajax').live('click', function(event) {
event.preventDefault();
if ($.active) return;
$.ajax({
url: this.href,
success: $.nette.success,
error: $.nette.error,
dataType: 'json',
headers: { 'X-DumpExceptions': 1 } // requires https://github.com/juzna/nette/commit/d5daa188bd6768b4f8bac42a51eb13a9c3e406cb
});
$.nette.spinner.css({
position: 'absolute',
left: event.pageX,
top: event.pageY
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment