diff --git a/packages/ember-htmlbars/lib/compat/helper.js b/packages/ember-htmlbars/lib/compat/helper.js index 31f859cd7f2..8acdbd3534d 100644 --- a/packages/ember-htmlbars/lib/compat/helper.js +++ b/packages/ember-htmlbars/lib/compat/helper.js @@ -22,12 +22,21 @@ var slice = [].slice; */ function HandlebarsCompatibleHelper(fn) { this.helperFunction = function helperFunc(params, hash, options, env) { - var param; + var param, blockResult, fnResult; + var context = this; var handlebarsOptions = {}; + merge(handlebarsOptions, options); merge(handlebarsOptions, env); handlebarsOptions.hash = {}; + + if (options.isBlock) { + handlebarsOptions.fn = function() { + blockResult = options.template.render(context, env, options.morph.contextualElement); + }; + } + for (var prop in hash) { param = hash[prop]; @@ -50,7 +59,9 @@ function HandlebarsCompatibleHelper(fn) { } args.push(handlebarsOptions); - return fn.apply(this, args); + fnResult = fn.apply(this, args); + + return options.isBlock ? blockResult : fnResult; }; this.isHTMLBars = true; diff --git a/packages/ember-htmlbars/tests/compat/helper_test.js b/packages/ember-htmlbars/tests/compat/helper_test.js index ccaf316f50f..5e912761dc5 100644 --- a/packages/ember-htmlbars/tests/compat/helper_test.js +++ b/packages/ember-htmlbars/tests/compat/helper_test.js @@ -141,5 +141,66 @@ test('registering a helper created from `Ember.Handlebars.makeViewHelper` does n equal(view.$().text(), 'woot!'); }); +test('does not add `options.fn` if no block was specified', function() { + expect(1); + + function someHelper(options) { + ok(!options.fn, '`options.fn` is not present when block is not specified'); + } + + registerHandlebarsCompatibleHelper('test', someHelper); + + view = EmberView.create({ + controller: { + value: 'foo' + }, + template: compile('{{test}}') + }); + + runAppend(view); +}); + +test('does not return helper result if block was specified', function() { + expect(1); + + function someHelper(options) { + return 'asdf'; + } + + registerHandlebarsCompatibleHelper('test', someHelper); + + view = EmberView.create({ + controller: { + value: 'foo' + }, + template: compile('{{#test}}lkj;{{/test}}') + }); + + runAppend(view); + + equal(view.$().text(), ''); +}); + +test('allows usage of the template fn', function() { + expect(1); + + function someHelper(options) { + options.fn(); + } + + registerHandlebarsCompatibleHelper('test', someHelper); + + view = EmberView.create({ + controller: { + value: 'foo' + }, + template: compile('{{#test}}foo{{/test}}') + }); + + runAppend(view); + + equal(view.$().text(), 'foo'); +}); + // jscs:enable validateIndentation }