Skip to content

Commit

Permalink
better payload status and javascript behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
chemix committed Jun 24, 2014
1 parent 5901b40 commit a13d6a0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/presenters/AdminPresenter.php
Expand Up @@ -27,6 +27,8 @@ public function actionEnablePost($postId)
if ($this->wallposts->enablePost($postId)) {
if ($this->isAjax()) {
$this->payload->message = 'Post enabled';
$this->payload->action = 'enable';
$this->payload->status = '1';
$this->sendPayload();

} else {
Expand All @@ -42,6 +44,8 @@ public function actionDisablePost($postId)
if ($this->wallposts->disablePost($postId)) {
if ($this->isAjax()) {
$this->payload->message = 'Post disabled';
$this->payload->action = 'disable';
$this->payload->status = '1';
$this->sendPayload();

} else {
Expand Down
21 changes: 17 additions & 4 deletions www/js/main.js
@@ -1,13 +1,20 @@
(function ($, undefined) {

// When Ready
$(function() {
$(function () {
$('#wall').masonry({
itemSelector: '.brick',
columnWidth: 320
});
});

var flashMessage = function(message)
{
console.log(message)
console.log($('body')[0])
$($('body')[0]).prepend($('<div class="flash info">'+message+'</div>'));
}


// Ajax click in admin
$('body').on('click', 'a.ajax', function (event) {
Expand All @@ -23,14 +30,20 @@
link.css('cursor', 'wait');
link.addClass('disabled');

$.post(this.href, function (data) {
if (data.message == 'Post disabled') {
$.post(this.href, function (payload) {
if (payload.action == 'disable' && payload.status == '1') {
// disabled
link.parent().parent().find('.disable').addClass('hide');
link.parent().parent().find('.enable').removeClass('hide');
} else {
flashMessage(payload.message);
} else if (payload.action == 'enable' && payload.status == '1') {
// enabled
link.parent().parent().find('.disable').removeClass('hide');
link.parent().parent().find('.enable').addClass('hide');
flashMessage(payload.message);
} else {
// error ? refresh...
window.location.href = window.location.href + '?refresh=' + Date.now();
}
link.removeClass('disabled');
link.css('cursor', 'default');
Expand Down

0 comments on commit a13d6a0

Please sign in to comment.