Keep global scope clean with AMD. Always check for jQuery global.

If an AMD environment, don't export `UAParser` to global scope.
Even in AMD environment jQuery exports to global scope. We should catch it even then.
It also can happen then one joins the jQuery source file with other jQuery plugins in a CommonJS env.
This commit is contained in:
Dumitru Uzun 2014-11-20 12:32:31 +02:00
parent 0951cebd09
commit 551fbf100c

View File

@ -739,30 +739,37 @@
} }
exports.UAParser = UAParser; exports.UAParser = UAParser;
} else { } else {
// browser env // requirejs env
window.UAParser = UAParser;
// requirejs env (optional)
if (typeof(define) === FUNC_TYPE && define.amd) { if (typeof(define) === FUNC_TYPE && define.amd) {
define(function () { define(function () {
return UAParser; return UAParser;
}); });
} }
// jQuery/Zepto specific (optional) else {
var $ = window.jQuery || window.Zepto; // browser env
if (typeof($) !== UNDEF_TYPE) { window.UAParser = UAParser;
var parser = new UAParser();
$.ua = parser.getResult();
$.ua.get = function() {
return parser.getUA();
};
$.ua.set = function (uastring) {
parser.setUA(uastring);
var result = parser.getResult();
for (var prop in result) {
$.ua[prop] = result[prop];
}
};
} }
} }
// jQuery/Zepto specific (optional)
// Note:
// In AMD env the global scope should be kept clean, but jQuery is an exception.
// jQuery always exports to global scope, unless jQuery.noConflict(true) is used,
// and we should catch that.
var $ = window.jQuery || window.Zepto;
if (typeof($) !== UNDEF_TYPE) {
var parser = new UAParser();
$.ua = parser.getResult();
$.ua.get = function() {
return parser.getUA();
};
$.ua.set = function (uastring) {
parser.setUA(uastring);
var result = parser.getResult();
for (var prop in result) {
$.ua[prop] = result[prop];
}
};
}
})(this); })(this);