Skip to content

Commit

Permalink
Merge pull request #10279 from rwjblue/ensure-compat-helper-functions…
Browse files Browse the repository at this point in the history
…-work

[BUGFIX beta] Ensure compat helpers calling options.fn work.
  • Loading branch information
rwjblue committed Jan 25, 2015
2 parents 968b6bc + 4a41fa5 commit 2477a7b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/ember-htmlbars/lib/compat/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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;
Expand Down
61 changes: 61 additions & 0 deletions packages/ember-htmlbars/tests/compat/helper_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 2477a7b

Please sign in to comment.