Comments (11)
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.
+1
from pubsubjs.
That looks like a RequireJS configuration issue to me.
Did you get it working in the end?
from pubsubjs.
+1
PubSub object is available only in factory function.
from pubsubjs.
@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.
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.
@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.
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.
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.
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.
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)
- Allow passing array of subscription tokens into unsubscribe
- Allow subscribing to multiple topics at once with same callback HOT 1
- Import vs window - unpredictable publishers HOT 5
- countSubscriptions always returns 1 HOT 4
- getSubscriptions wrong result
- use it in react hooks Why are requests repeated HOT 3
- PubSub does not work under Context API HOT 4
- Add "files" field in package.json HOT 1
- PubSub keep using the initial state value in .subscribe() - ReactJS HOT 4
- Any tests examples approach on this library? (with React) HOT 2
- Add the possibility to await sync events (publishAwait) HOT 3
- I can't subscribe event while publish any topic, in different controller
- Rename "PubSub" variable for multiple instances HOT 1
- Feature wanted - RegExp HOT 1
- clearSubscriptions would clear different topics which includes the same string
- ReadMe points to wrong jsdelivr package HOT 5
- Publish Latest value HOT 2
- Wildcard support for subscriptions? HOT 2
- Troubleshooting event subscription not working in a React component HOT 1
- not working on k8s HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pubsubjs.