Implement decorator environment and registration
This commit is contained in:
+16
-1
@@ -1,6 +1,7 @@
|
||||
import {createFrame, extend, toString} from './utils';
|
||||
import Exception from './exception';
|
||||
import {registerDefaultHelpers} from './helpers';
|
||||
import {registerDefaultDecorators} from './decorators';
|
||||
import logger from './logger';
|
||||
|
||||
export const VERSION = '3.0.1';
|
||||
@@ -17,11 +18,13 @@ export const REVISION_CHANGES = {
|
||||
|
||||
const objectType = '[object Object]';
|
||||
|
||||
export function HandlebarsEnvironment(helpers, partials) {
|
||||
export function HandlebarsEnvironment(helpers, partials, decorators) {
|
||||
this.helpers = helpers || {};
|
||||
this.partials = partials || {};
|
||||
this.decorators = decorators || {};
|
||||
|
||||
registerDefaultHelpers(this);
|
||||
registerDefaultDecorators(this);
|
||||
}
|
||||
|
||||
HandlebarsEnvironment.prototype = {
|
||||
@@ -54,6 +57,18 @@ HandlebarsEnvironment.prototype = {
|
||||
},
|
||||
unregisterPartial: function(name) {
|
||||
delete this.partials[name];
|
||||
},
|
||||
|
||||
registerDecorator: function(name, fn) {
|
||||
if (toString.call(name) === objectType) {
|
||||
if (fn) { throw new Exception('Arg not supported with multiple decorators'); }
|
||||
extend(this.decorators, name);
|
||||
} else {
|
||||
this.decorators[name] = fn;
|
||||
}
|
||||
},
|
||||
unregisterDecorator: function(name) {
|
||||
delete this.decorators[name];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import registerInline from './decorators/inline';
|
||||
|
||||
export function registerDefaultDecorators(instance) {
|
||||
}
|
||||
|
||||
@@ -153,9 +153,13 @@ export function template(templateSpec, env) {
|
||||
if (templateSpec.usePartial) {
|
||||
container.partials = container.merge(options.partials, env.partials);
|
||||
}
|
||||
if (templateSpec.useDecorators) {
|
||||
container.decorators = container.merge(options.decorators, env.decorators);
|
||||
}
|
||||
} else {
|
||||
container.helpers = options.helpers;
|
||||
container.partials = options.partials;
|
||||
container.decorators = options.decorators;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -166,4 +166,44 @@ describe('blocks', function() {
|
||||
shouldCompileTo(string, [hash, undefined, undefined, true], 'Goodbye cruel ');
|
||||
});
|
||||
});
|
||||
|
||||
describe('decorators', function() {
|
||||
describe('registration', function() {
|
||||
it('unregisters', function() {
|
||||
handlebarsEnv.decorators = {};
|
||||
|
||||
handlebarsEnv.registerDecorator('foo', function() {
|
||||
return 'fail';
|
||||
});
|
||||
|
||||
equals(!!handlebarsEnv.decorators.foo, true);
|
||||
handlebarsEnv.unregisterDecorator('foo');
|
||||
equals(handlebarsEnv.decorators.foo, undefined);
|
||||
});
|
||||
|
||||
it('allows multiple globals', function() {
|
||||
handlebarsEnv.decorators = {};
|
||||
|
||||
handlebarsEnv.registerDecorator({
|
||||
foo: function() {},
|
||||
bar: function() {}
|
||||
});
|
||||
|
||||
equals(!!handlebarsEnv.decorators.foo, true);
|
||||
equals(!!handlebarsEnv.decorators.bar, true);
|
||||
handlebarsEnv.unregisterDecorator('foo');
|
||||
handlebarsEnv.unregisterDecorator('bar');
|
||||
equals(handlebarsEnv.decorators.foo, undefined);
|
||||
equals(handlebarsEnv.decorators.bar, undefined);
|
||||
});
|
||||
it('fails with multiple and args', function() {
|
||||
shouldThrow(function() {
|
||||
handlebarsEnv.registerDecorator({
|
||||
world: function() { return 'world!'; },
|
||||
testHelper: function() { return 'found it!'; }
|
||||
}, {});
|
||||
}, Error, 'Arg not supported with multiple decorators');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user