Skip to content

Commit

Permalink
Provide Handlebars.noConflict implementation
Browse files Browse the repository at this point in the history
Allows for users who are loading via a global pattern to avoid conflicting with other instances of the library.

Fixes #887
  • Loading branch information
kpdecker committed Nov 26, 2014
1 parent 617dd57 commit c4b3c90
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 9 additions & 0 deletions lib/handlebars.runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion spec/env/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 3 additions & 1 deletion spec/env/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ 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;
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) {
Expand Down
18 changes: 18 additions & 0 deletions spec/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});

0 comments on commit c4b3c90

Please sign in to comment.