Skip to content

Commit

Permalink
Merge pull request #11445 from mixonic/helper-bug
Browse files Browse the repository at this point in the history
[BUGFIX release] Allow recomputation for helpers with arguments
  • Loading branch information
mixonic committed Jun 15, 2015
2 parents 0ad92a6 + 7ace109 commit 4d13ef9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/ember-htmlbars/lib/hooks/invoke-helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ember from 'ember-metal/core'; // Ember.assert
import { buildHelperStream } from "ember-htmlbars/system/invoke-helper";
import subscribe from "ember-htmlbars/utils/subscribe";

export default function invokeHelper(morph, env, scope, visitor, params, hash, helper, templates, context) {

Expand All @@ -16,6 +17,26 @@ export default function invokeHelper(morph, env, scope, visitor, params, hash, h

// Ember.Helper helpers are pure values, thus linkable
if (helperStream.linkable) {

if (morph) {
// When processing an inline expression the params and hash have already
// been linked. Thus, HTMLBars will not link the returned helperStream.
// We subscribe the morph to the helperStream here, and also subscribe
// the helperStream to any params.
let addedDependency = false;
for (var i = 0, l = params.length; i < l; i++) {
addedDependency = true;
helperStream.addDependency(params[i]);
}
for (var key in hash) {
addedDependency = true;
helperStream.addDependency(hash[key]);
}
if (addedDependency) {
subscribe(morph, env, scope, helperStream);
}
}

return { link: true, value: helperStream };
}

Expand Down
32 changes: 32 additions & 0 deletions packages/ember-htmlbars/tests/helpers/custom_helper_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,38 @@ QUnit.test('dashed helper can recompute a new value', function() {
equal(destroyCount, 0, 'destroy is not called on recomputation');
});

QUnit.test('dashed helper with arg can recompute a new value', function() {
var destroyCount = 0;
var count = 0;
var helper;
var HelloWorld = Helper.extend({
init() {
this._super(...arguments);
helper = this;
},
compute() {
return ++count;
},
destroy() {
destroyCount++;
this._super();
}
});
registry.register('helper:hello-world', HelloWorld);
component = Component.extend({
container,
layout: compile('{{hello-world "whut"}}')
}).create();

runAppend(component);
equal(component.$().text(), '1');
run(function() {
helper.recompute();
});
equal(component.$().text(), '2');
equal(destroyCount, 0, 'destroy is not called on recomputation');
});

QUnit.test('dashed shorthand helper is called for param changes', function() {
var count = 0;
var HelloWorld = makeHelper(function() {
Expand Down

0 comments on commit 4d13ef9

Please sign in to comment.