diff --git a/lib/handlebars.js b/lib/handlebars.js index 039ab3e17..5a4b301ae 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -30,6 +30,15 @@ var create = function() { Handlebars = create(); Handlebars.create = create; +/*jshint -W040 */ +var root = typeof global !== 'undefined' ? global : window, + $Handlebars = root.Handlebars; +Handlebars.noConflict = function() { + if (root.Handlebars === Handlebars) { + root.Handlebars = $Handlebars; + } +}; + Handlebars['default'] = Handlebars; export default Handlebars; diff --git a/lib/handlebars.runtime.js b/lib/handlebars.runtime.js index bc07714f2..49f29632f 100644 --- a/lib/handlebars.runtime.js +++ b/lib/handlebars.runtime.js @@ -29,6 +29,15 @@ var create = function() { var Handlebars = create(); Handlebars.create = create; +/*jshint -W040 */ +var root = typeof global !== 'undefined' ? global : window, + $Handlebars = root.Handlebars; +Handlebars.noConflict = function() { + if (root.Handlebars === Handlebars) { + root.Handlebars = $Handlebars; + } +}; + Handlebars['default'] = Handlebars; export default Handlebars; diff --git a/spec/env/browser.js b/spec/env/browser.js index a0ce2431b..a9042e3fb 100644 --- a/spec/env/browser.js +++ b/spec/env/browser.js @@ -5,7 +5,7 @@ var _ = require('underscore'), fs = require('fs'), vm = require('vm'); -global.Handlebars = undefined; +global.Handlebars = 'no-conflict'; vm.runInThisContext(fs.readFileSync(__dirname + '/../../dist/handlebars.js'), 'dist/handlebars.js'); global.CompilerContext = { diff --git a/spec/env/runtime.js b/spec/env/runtime.js index 5a2dcd9b3..0c144064e 100644 --- a/spec/env/runtime.js +++ b/spec/env/runtime.js @@ -5,7 +5,7 @@ var _ = require('underscore'), fs = require('fs'), vm = require('vm'); -global.Handlebars = undefined; +global.Handlebars = 'no-conflict'; vm.runInThisContext(fs.readFileSync(__dirname + '/../../dist/handlebars.runtime.js'), 'dist/handlebars.runtime.js'); var parse = require('../../dist/cjs/handlebars/compiler/base').parse; @@ -13,6 +13,8 @@ var compiler = require('../../dist/cjs/handlebars/compiler/compiler'); var JavaScriptCompiler = require('../../dist/cjs/handlebars/compiler/javascript-compiler')['default']; global.CompilerContext = { + browser: true, + compile: function(template, options) { // Hack the compiler on to the environment for these specific tests handlebarsEnv.precompile = function(template, options) { diff --git a/spec/runtime.js b/spec/runtime.js index d33dd747c..48a22a99b 100644 --- a/spec/runtime.js +++ b/spec/runtime.js @@ -60,4 +60,22 @@ describe('runtime', function() { equal(template._child(1, undefined, [{bar: 'baz'}])(), 'baz'); }); }); + + describe('#noConflict', function() { + if (!CompilerContext.browser) { + return; + } + + it('should reset on no conflict', function() { + var reset = Handlebars; + Handlebars.noConflict(); + equal(Handlebars, 'no-conflict'); + + Handlebars = 'really, none'; + reset.noConflict(); + equal(Handlebars, 'really, none'); + + Handlebars = reset; + }); + }); });