mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-04-12 19:11:05 +00:00
Added noConflict method and test
This commit is contained in:
parent
f4787f506b
commit
ab978b9d3e
7 changed files with 48 additions and 7 deletions
|
|
@ -11,7 +11,7 @@
|
|||
// Node, or CommonJS-Like environments
|
||||
module.exports = function (_) {
|
||||
_ = _ || require("underscore");
|
||||
return factory(_);
|
||||
return factory(_, this);
|
||||
};
|
||||
} else if (typeof define === "function" && define.amd) {
|
||||
// AMD. Register as an anonymous module.
|
||||
|
|
@ -25,6 +25,7 @@
|
|||
}(this, function (_, global, undefined) {
|
||||
|
||||
var postal;
|
||||
var prevPostal = global.postal;
|
||||
|
||||
|
||||
var ConsecutiveDistinctPredicate = function () {
|
||||
|
|
@ -424,6 +425,14 @@
|
|||
return result;
|
||||
},
|
||||
|
||||
noConflict: function () {
|
||||
if (typeof window === "undefined") {
|
||||
throw new Error("noConflict can only be used in browser clients which aren't using AMD modules");
|
||||
}
|
||||
global.postal = prevPostal;
|
||||
return this;
|
||||
},
|
||||
|
||||
utils: {
|
||||
getSubscribersFor: function () {
|
||||
var channel = arguments[0],
|
||||
|
|
|
|||
2
lib/postal.min.js
vendored
2
lib/postal.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -6,6 +6,10 @@
|
|||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script type="text/javascript">
|
||||
// initial postal value for a noConflict test....
|
||||
window.postal = { foo: "bar" };
|
||||
</script>
|
||||
<script type="text/javascript" src="../bower/jquery/jquery.min.js"></script>
|
||||
<script type="text/javascript" src="../bower/underscore/underscore-min.js"></script>
|
||||
<script type="text/javascript" src="../bower/expect/expect.js"></script>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/* global describe, postal, it, after, before, expect */
|
||||
(function() {
|
||||
(function(global) {
|
||||
var postal = typeof window === "undefined" ? require("../lib/postal.js")() : window.postal;
|
||||
var expect = typeof window === "undefined" ? require("expect.js") : window.expect;
|
||||
var _ = typeof window === "undefined" ? require("underscore") : window._;
|
||||
|
|
@ -9,6 +9,25 @@
|
|||
var caughtSubscribeEvent = false;
|
||||
var caughtUnsubscribeEvent = false;
|
||||
|
||||
describe("noConflict", function() {
|
||||
it("should return control to the previous postal value", function() {
|
||||
if(typeof window === "undefined" || (typeof window !== "undefined" && typeof require === "function" && define.amd)) {
|
||||
var err = false;
|
||||
try {
|
||||
postal.noConflict();
|
||||
} catch(e) {
|
||||
err = true;
|
||||
}
|
||||
expect(err).to.be( true );
|
||||
} else {
|
||||
var _postal = global.postal; // hang on to postal value
|
||||
postal.noConflict(); // return previous postal
|
||||
expect( global.postal.foo ).to.be( "bar" );
|
||||
global.postal = _postal; // return postal back as it was
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("subscription creation", function(){
|
||||
describe( "When creating basic subscription", function () {
|
||||
var systemSubscription = {};
|
||||
|
|
@ -694,4 +713,4 @@
|
|||
} );
|
||||
} );
|
||||
});
|
||||
}());
|
||||
}(this));
|
||||
10
src/Api.js
10
src/Api.js
|
|
@ -1,4 +1,4 @@
|
|||
/* global localBus, bindingsResolver, ChannelDefinition, SubscriptionDefinition, postal */
|
||||
/* global localBus, bindingsResolver, ChannelDefinition, SubscriptionDefinition, postal, prevPostal, global */
|
||||
/*jshint -W020 */
|
||||
postal = {
|
||||
configuration : {
|
||||
|
|
@ -54,6 +54,14 @@ postal = {
|
|||
return result;
|
||||
},
|
||||
|
||||
noConflict: function() {
|
||||
if(typeof window === "undefined") {
|
||||
throw new Error("noConflict can only be used in browser clients which aren't using AMD modules");
|
||||
}
|
||||
global.postal = prevPostal;
|
||||
return this;
|
||||
},
|
||||
|
||||
utils : {
|
||||
getSubscribersFor : function () {
|
||||
var channel = arguments[ 0 ],
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ var localBus = {
|
|||
unSubQueue.push( config );
|
||||
return;
|
||||
}
|
||||
if ( this.subscriptions[config.channel][config.topic] ) {
|
||||
if ( this.subscriptions[config.channel] && this.subscriptions[config.channel][config.topic] ) {
|
||||
var len = this.subscriptions[config.channel][config.topic].length,
|
||||
idx = 0;
|
||||
while ( idx < len ) {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
// Node, or CommonJS-Like environments
|
||||
module.exports = function ( _ ) {
|
||||
_ = _ || require( "underscore" );
|
||||
return factory( _ );
|
||||
return factory( _, this );
|
||||
};
|
||||
} else if ( typeof define === "function" && define.amd ) {
|
||||
// AMD. Register as an anonymous module.
|
||||
|
|
@ -18,6 +18,7 @@
|
|||
}( this, function ( _, global, undefined ) {
|
||||
|
||||
var postal;
|
||||
var prevPostal = global.postal;
|
||||
|
||||
//import("ConsecutiveDistinctPredicate.js");
|
||||
//import("DistinctPredicate.js");
|
||||
|
|
|
|||
Loading…
Reference in a new issue