Coder Social home page Coder Social logo

Problem use in requirejs about pubsubjs HOT 11 OPEN

mroderick avatar mroderick commented on June 10, 2024
Problem use in requirejs

from pubsubjs.

Comments (11)

bjrn avatar bjrn commented on June 10, 2024 1

This change means that it's no longer possible to use PubSubJS without modification if one would like to avoid leaking global vars. Even though it's a small task to remove the global assign from the library when it's not desired – is unconditionally assigning window.PubSub the most common use-case?

from pubsubjs.

mikkotikkanen avatar mikkotikkanen commented on June 10, 2024

+1

from pubsubjs.

mroderick avatar mroderick commented on June 10, 2024

That looks like a RequireJS configuration issue to me.

Did you get it working in the end?

from pubsubjs.

iborisov avatar iborisov commented on June 10, 2024

+1
PubSub object is available only in factory function.

from pubsubjs.

aron avatar aron commented on June 10, 2024

@mroderick the jQuery shim expects PubSub to be a global, but since introducing the new UMD wrapper it isn't so you need to check for and require jQuery in AMD and Node environments. e.g.:

  function createPlugin(jQuery, PubSub) {
    // Create the pub sub methods.
  }

  if (typeof define === 'function' && define.amd){
        require(['jquery', 'pubsub'], createPlugin);
    } else if (typeof exports === 'object'){
        // CommonJS
        createPlugin(require('jquery'), require('pubsub'));
    } else {
        // Browser globals
        createPlugin(window.jQuery, window.PubSub);
    }

from pubsubjs.

mroderick avatar mroderick commented on June 10, 2024

I've removed the line that calculates the version property for jQuery with 2d171ae as the core property was removed a long time ago.

from pubsubjs.

mroderick avatar mroderick commented on June 10, 2024

@aron I've also created a pull request to show that it is possible to load the jQuery built version in an AMD context: #65.

Does this cover the scenario you described?

from pubsubjs.

iborisov avatar iborisov commented on June 10, 2024

You define it as a module for require.js, but the point is to use it as "regular" jQuery plugin.
In my project I load all jQuery plugins at once at application startup like:

define(['jquery', 'jquery.plugin1', 'jquery.plugin2'], function($) {
  // then use plugin1 and plugin2 from jquery:
  $.plugin1(...);
  $.plugin2(...);
}

All plugins I'm using except of jquery.pubsub work this way.

from pubsubjs.

amclin avatar amclin commented on June 10, 2024

Here's a simple test:

require(['jquery','pubsub'],function($,pubsub) {
  console.log(typeof(pubsub)); // Returns object
  console.log(typeof(PubSub)); // Returns undefined
  console.log(typeof($.pubsub)); // Returns function
  $.pubsub('subscribe',"foo", bar); // PubSub is undefined
});

When using AMD and the JQuery version, the pubsub module binds a useless method to the JQuery object because the globally scoped variables are never created.

I don't think is a realistic expectation that every time someone includes PubSub via require.js defines they need to also manually attach the object and define the globals.

This can be solved by removing the else wrapper around

// Browser globals
var PubSub = {};
root.PubSub = PubSub;
factory(PubSub);

from pubsubjs.

amclin avatar amclin commented on June 10, 2024

I would argue that yes, like JQuery, a globally namespaced object is the most common use case considering the events themselves can be namespaced. Although I do think there's a valid argument for a local object, I believe that should be handled by argument/configuration as it would be the exception, not the norm.

from pubsubjs.

bjrn avatar bjrn commented on June 10, 2024

Just seems like always exposing a global object goes against the purpose of using AMD in the first place. If I want to expose window.PubSub I would just require the library and assign it in the beginning of the app.

Also, this "feature" was added in a patch release and had some unfortunate side-effects. I can agree however that our usage is probably less common and have no issue with manually patching the lib for our purpose.

from pubsubjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.