inherit
252032
0
Sept 18, 2023 9:07:51 GMT -8
Retread
Tribbial Pursuit.
5,014
January 2018
retread
|
Post by Retread on Sept 9, 2019 6:38:49 GMT -8
I would not recommend having 2 $(document).ready calls - which is why I suggested making them into functions, like so: I'm willing to take it on faith that having two $(document).ready calls is a bad thing, but it it's not too difficult to explain why, could you do so or link me to something that leads to a greater understanding? I thought $(document).ready was just a check to insure the document was fully loaded before proceeding. Apparently there's more to it than that?
|
|
inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,768
January 2015
msg
|
Post by Lynx on Sept 9, 2019 9:10:22 GMT -8
I would not recommend having 2 $(document).ready calls - which is why I suggested making them into functions, like so: I'm willing to take it on faith that having two $(document).ready calls is a bad thing, but it it's not too difficult to explain why, could you do so or link me to something that leads to a greater understanding? I thought $(document).ready was just a check to insure the document was fully loaded before proceeding. Apparently there's more to it than that? Bad? No. More closer to poor programming practice than actually being bad - since you can have more than 1 document.ready in your code [1] (be sure to read all of the answers, particularly the 3rd one - click the superscript 1 for link). Since you basically have 2 functions, that's why I made the suggested change. Now, with those parts as functions, they can easily be called with just 1 document.ready. But, as the article says, there's actually no harm in having 2 document.ready in your code. The main point (imho) for only having 1 document.ready is for readability. For a short code like yours, it's not too bad. Imagine having a few hundred lines of code with a bunch of document.ready calls in there. That would just be confusing to me. But, this is just my opinion.
|
|
inherit
252032
0
Sept 18, 2023 9:07:51 GMT -8
Retread
Tribbial Pursuit.
5,014
January 2018
retread
|
Post by Retread on Sept 9, 2019 10:20:26 GMT -8
be sure to read all of the answers, particularly the 3rd one I took a quick look and among other things it also seems a matter of efficient use of resources. I'll look more closely later when I have more time. I'll also post what I think currently think might be a streamlined way of segmenting this, while using a single call. Probably later today or perhaps early tomorrow. Being new to this, your advice on accepted practices is greatly appreciated.
|
|
inherit
252032
0
Sept 18, 2023 9:07:51 GMT -8
Retread
Tribbial Pursuit.
5,014
January 2018
retread
|
Post by Retread on Sept 9, 2019 16:23:31 GMT -8
Lynx , if you have a moment or two I'd be grateful for your opinion on this: $(document).ready(function() { // Selection from Threads Autoform if (pb.plugin.get('remove_mp_on_first_post').settings.thread_id_numbers.length) { for (var z = 0; z < pb.plugin.get('remove_mp_on_first_post').settings.thread_id_numbers.length; z++) { if (proboards.data('thread_id') == parseInt(pb.plugin.get('remove_mp_on_first_post').settings.thread_id_numbers[z].thread_id)) { $('.posts .post.first .left-panel').css('display', 'none'); if (pb.plugin.get('remove_mp_on_first_post').settings.thread_id_numbers[z].xsig == 1){ $('.posts .post.first .foot .signature').css('display', 'none'); } if (pb.plugin.get('remove_mp_on_first_post').settings.thread_id_numbers[z].xedit == 1){ $('.posts .post.first .foot .edited_by').css('display', 'none'); } if (pb.plugin.get('remove_mp_on_first_post').settings.thread_id_numbers[z].xquot == 1){ $('.posts .post.first .button.quote-button').css('display', 'none'); } }}; } // Selection from Boards Autoform if (pb.plugin.get('remove_mp_on_first_post').settings.board_id_numbers.length) { for (var z = 0; z < pb.plugin.get('remove_mp_on_first_post').settings.board_id_numbers.length; z++) { if (proboards.data('board_id') == parseInt(pb.plugin.get('remove_mp_on_first_post').settings.board_id_numbers[z].board_id)) { $('.posts .post.first .left-panel').css('display', 'none'); if (pb.plugin.get('remove_mp_on_first_post').settings.board_id_numbers[z].xsig == 1){ $('.posts .post.first .foot .signature').css('display', 'none'); } if (pb.plugin.get('remove_mp_on_first_post').settings.board_id_numbers[z].xedit == 1){ $('.posts .post.first .foot .edited_by').css('display', 'none'); } if (pb.plugin.get('remove_mp_on_first_post').settings.board_id_numbers[z].xquote == 1){ $('.posts .post.first .button.quote-button').css('display', 'none'); } }}; } });
|
|
inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,768
January 2015
msg
|
Post by Lynx on Sept 10, 2019 7:44:34 GMT -8
That will certainly work, Retread. Personally, with what you have, I would move to using variables (this is solely my personal preference) just to help with readability. This is how I (personally) would have changed it to (had I continued with it):
$(document).ready(function() {
var settings = pb.plugin.get('remove_mp_on_first_post').settings;
// Selection from Threads Autoform if (settings.thread_id_numbers.length) { for (var z = 0; z < settings.thread_id_numbers.length; z++) { if (proboards.data('thread_id') == parseInt(settings.thread_id_numbers[z].thread_id)) { $('.posts .post.first .left-panel').css('display', 'none'); if (settings.thread_id_numbers[z].xsig == 1){ $('.posts .post.first .foot .signature').css('display', 'none'); } if (settings.thread_id_numbers[z].xedit == 1){ $('.posts .post.first .foot .edited_by').css('display', 'none'); } if (settings.thread_id_numbers[z].xquot == 1){ $('.posts .post.first .button.quote-button').css('display', 'none'); } }}; } // Selection from Boards Autoform if (settings.board_id_numbers.length) { for (var z = 0; z < settings.board_id_numbers.length; z++) { if (proboards.data('board_id') == parseInt(settings.board_id_numbers[z].board_id)) { $('.posts .post.first .left-panel').css('display', 'none'); if (settings.board_id_numbers[z].xsig == 1){ $('.posts .post.first .foot .signature').css('display', 'none'); } if (settings.board_id_numbers[z].xedit == 1){ $('.posts .post.first .foot .edited_by').css('display', 'none'); } if (settings.board_id_numbers[z].xquote == 1){ $('.posts .post.first .button.quote-button').css('display', 'none'); } }}; } });
Again, this is only a personal preference of mine to help with readability (less typing too). All I did was let the settings variable take the place of every pb.plugin.get('remove_mp_on_first_post').settings call. I'm not a big fan of typing ... which is why I probably use voice chat whenever I can, so the less typing I have to do for my code, the better.
|
|
inherit
252032
0
Sept 18, 2023 9:07:51 GMT -8
Retread
Tribbial Pursuit.
5,014
January 2018
retread
|
Post by Retread on Sept 10, 2019 8:22:04 GMT -8
Whoa! That is sweet. I'll do that and add that idea to my toolkit.
|
|