Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem use in requirejs #35

Open
Troland opened this issue Nov 20, 2013 · 11 comments
Open

Problem use in requirejs #35

Troland opened this issue Nov 20, 2013 · 11 comments

Comments

@Troland
Copy link

Troland commented Nov 20, 2013

It's weired that when i run rake jquery to get a jquery pubsub version.After that, i use it in requriejs version 2.1.8+ where the config is like

requirejs.config({
        'paths': {
            'jquery': 'jquery-1.10.2.min',
            'pubsub': 'jquery.pubsub'
        },
        'shim': {
            'pubsub': ['jquery']
        }
});

it produced a error said

PubSub is not defined` in line `pubsub.version = PubSub.name + ' ' + PubSub.version;

It seem that the the line 38
``
if (typeof exports === 'object' && module){
module.exports = factory();

// AMD
} else if (typeof define === 'function' && define.amd){
    define(factory);
// Browser
} else {
    root.PubSub = factory();
}
@mikkotikkanen
Copy link

+1

@mroderick
Copy link
Owner

That looks like a RequireJS configuration issue to me.

Did you get it working in the end?

@iborisov
Copy link

+1
PubSub object is available only in factory function.

@aron
Copy link
Collaborator

aron commented Sep 1, 2014

@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);
    }

@aron aron reopened this Sep 1, 2014
@mroderick
Copy link
Owner

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

@mroderick
Copy link
Owner

@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?

@iborisov
Copy link

iborisov commented Sep 2, 2014

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.

@amclin
Copy link
Contributor

amclin commented May 14, 2015

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);

@bjrn
Copy link

bjrn commented Mar 15, 2016

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?

@amclin
Copy link
Contributor

amclin commented Mar 30, 2016

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.

@bjrn
Copy link

bjrn commented Mar 30, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants