scripting / feedlandBlogrollToolkit

Browser-based JavaScript code, html and styles for adding an OPML-based blogroll to a website, with a connection back to FeedLand for realtime updates.
MIT License
3 stars 0 forks source link

jQuery no conflict mode #1

Closed NickGreen closed 5 months ago

NickGreen commented 5 months ago

WordPress runs jQuery in no conflict mode. In our code, we've done it a little less than ideal for now, by reassigning $, like this: <div id="divBlogrollContainer"></div><script>$=jQuery;blogroll(BLOGROLL_OPTIONS);</script>'

It would probably be better to do it in a more standard way.

A couple of possible solutions moving forward:

Personally, I've tended to prefer the second option in my own code. There are examples in the jQuery docs, but it would look essentially like this:

(function( $ ) {
    // Your code using $ as alias to jQuery
})(jQuery);
scripting commented 5 months ago

@NickGreen -- greetings and thanks for posting this here. :-)

Would it help to add this to the beginning of the blogroll function?

function blogroll (userOptions) {
      const $ = jQuery.

      etc etc.
      }

I updated the blogroll code to do that at the beginning.

Does that help??

NickGreen commented 5 months ago

That is a simple workaround, and would certainly make the code work. It sort of depends on how "correct" you want to be (as defined by jQuery, in this case).

The main difference between this and true no conflict mode is that this is merely an alias within a specific scope. jQuery's no-conflict mode is a method designed to relinquish control of the global variable $ so that other libraries can use it.

Totally a choice.

scripting commented 5 months ago

I'll keep that in mind, certainly something we could do later, esp when there are more people working on this stuff. ;-)

NickGreen commented 5 months ago

Fernando, would you be able to confirm for me that this will work?

I'm still getting a JS error for code.js on line 2080 that $ is undefined here:

$(document).ready (function () {
fmfernandes commented 5 months ago

@NickGreen, @scripting 👋🏻

I'm also getting the error you mentioned in the above comment. I think that part of code.js was meant for use specifically for FeedLand, i.e. there's no #idPrefsDialog when using just the blogroll.

Without breaking anything I think we could do the following:

Change this:

$(document).ready (function () {
    $("#idPrefsDialog").bind ('show', function () {
        prefsSetDefaultValues ();
        });
    });

to

(function($) {
    $(document).ready (function () {
    $("#idPrefsDialog").bind ('show', function () {
        prefsSetDefaultValues ();
        });
    });
})(jQuery);

Of course, "without breaking anything" might be a bold statement and this needs testing, but it's the simplest solution I can think of. Leaving here just in case we come back to this issue later.

scripting commented 5 months ago

@NickGreen -- sorry I missed your comment from 3 days ago.

Took me a while to figure out where that is. :smile:

That code will never get called.

Is it getting called??

If so, can you get a stack crawl??

scripting commented 5 months ago

Basically I include a big file that has all my commonly-used routines. I could probably do a custom one for the blogroll package, but that's another thing to maintain, which is a problem I don't go looking for..

scripting commented 5 months ago

Never mind. I found it. That's a mistake. Now @fmfernandes comment is starting to make sense. ;-)

scripting commented 5 months ago

Ignore the comments above, I just fixed the problem, I believe. :smile:

I apologize for that. I wrote that code in 2014 when I was just making the transition from C to JavaScript.

If I were writing that today it would be organized like the blogroll code is, all in one function, with all its variables inside the function, so there couldn't be collisions.

In any case could you test the plugin and confirm that it doesn't get hung up there?

fmfernandes commented 5 months ago

That fixed it, @scripting. Thanks.