Do not call initialize_modules after every ajax call

We were calling initialize_modules after every ajax call only to apply the
application javascript to new elements added though ajax calls, this is not
always needed because some ajax calls do not add new elements to the user
interface so there is no new elements to initialize. This technique was working
fine in most cases but was causing different kind of problems at some pages where
some elements where being unnecessarily reinitiliazed causing the execution of the
element associated scripts as many times as the element was initialized. This fix
should relief a little bit of work to users browsers.

After this change we had to fix some pieces of javascript:

Regarding LegistationAnnotatable module, since we are not re-initializing Annotator
app we cannot destroy it so now we only need to insert the new annotation into annotator
interface to keep it properly updated.

Regarding Comments module, we do not need anymore the initialization check because this
code only will be fired once now we do not launch application initialization after ajax
calls. Also, when a new comment is created its added to the DOM through AJAX and include
some elements that needs javascript initialization to work. By using
"Delegated event handlers" [1] new comments will be initialized automatically when added.

[1] https://api.jquery.com/on/#direct-and-delegated-events
This commit is contained in:
Senén Rodero Rodríguez
2020-05-04 11:56:02 +02:00
parent c0e7eec882
commit 70a9fb7355
4 changed files with 57 additions and 18 deletions

View File

@@ -43,23 +43,18 @@
$("span#" + id + "_arrow").toggleClass("fa-minus-square").toggleClass("fa-plus-square");
},
initialize: function() {
$("body .js-add-comment-link").each(function() {
if ($(this).data("initialized") !== "yes") {
$(this).on("click", function() {
App.Comments.toggle_form($(this).data().id);
return false;
}).data("initialized", "yes");
}
$("body").on("click", ".js-add-comment-link", function() {
App.Comments.toggle_form($(this).data().id);
return false;
});
$("body .js-toggle-children").each(function() {
$(this).on("click", function() {
var children_container_id;
children_container_id = ($(this).data().id) + "_children";
$("#" + children_container_id).toggle("slow");
App.Comments.toggle_arrow(children_container_id);
$(this).children(".js-child-toggle").toggle();
return false;
});
$("body").on("click", ".js-toggle-children", function() {
var children_container_id;
children_container_id = ($(this).data().id) + "_children";
$("#" + children_container_id).toggle("slow");
App.Comments.toggle_arrow(children_container_id);
$(this).children(".js-child-toggle").toggle();
return false;
});
}
};