Skip to content

Commit

Permalink
Fix AJAX deletion and undo.
Browse files Browse the repository at this point in the history
This JS was not written to be bundled together, but with the new Rails
asset pipeline that is exactly what happens. As a result, multiple
conflicting behaviours were being added to each form. This commit
ensures that only the desired behaviour is applied to each page.

Fixes #50.
  • Loading branch information
xaviershay committed Feb 25, 2012
1 parent bd91490 commit 9e07aca
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 27 deletions.
3 changes: 0 additions & 3 deletions app/assets/javascripts/admin/comments.js

This file was deleted.

7 changes: 7 additions & 0 deletions app/assets/javascripts/admin/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,10 @@ function destroyAndUndoBehaviour(type) {
}
}

$(document).ready(function() {
$(['posts', 'comments', 'pages']).each(function() {
if ($('#' + this).length > 0) {
destroyAndUndoBehaviour(this)();
}
});
});
40 changes: 21 additions & 19 deletions app/assets/javascripts/admin/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ function extractId(obj) {
}

$(document).ready(function (){
$('.comment-body').hide();
if ($('#recent-comments').length) {
$('.comment-body').hide();

$('.comment-link').click (function() {
comment_body_id = '#comment-body-' + extractId($(this));
$('.comment-link').click (function() {
comment_body_id = '#comment-body-' + extractId($(this));

$('.comment-body').not(comment_body_id).hide();
$(comment_body_id).toggle();
$('.comment-body').not(comment_body_id).hide();
$(comment_body_id).toggle();

return false;
})
return false;
})

$(document).click(function() {
$('.comment-body').hide();
});
$(document).click(function() {
$('.comment-body').hide();
});

asyncUndoBehaviour();
asyncUndoBehaviour();

$('form.delete-item').submit(function () {
asyncDeleteForm($(this));
$('form.delete-item').submit(function () {
asyncDeleteForm($(this));

// Assume success and remove comment
comment_link_id = '#comment-link-' + extractId($(this));
$(comment_link_id).remove();
$(this).parent('div').parent('div').remove();
return false;
});
// Assume success and remove comment
comment_link_id = '#comment-link-' + extractId($(this));
$(comment_link_id).remove();
$(this).parent('div').parent('div').remove();
return false;
});
}
})
1 change: 0 additions & 1 deletion app/assets/javascripts/admin/pages.js

This file was deleted.

1 change: 0 additions & 1 deletion app/assets/javascripts/admin/posts.js

This file was deleted.

3 changes: 2 additions & 1 deletion app/views/admin/comments/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<h1>Comments</h1>
<table>

<table id='comments'>
<thead>
<tr>
<th>Created At</th>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/pages/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Your pages</h1>

<table>
<table id='pages'>
<thead>
<tr>
<th>Created At</th>
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<h1>Your posts</h1>
<table>

<table id='posts'>
<thead>
<tr>
<th>Published At</th>
Expand Down

0 comments on commit 9e07aca

Please sign in to comment.