Skip to content

Commit

Permalink
[BUGFIX beta] Ensure closure actions are wrapped in a run loop.
Browse files Browse the repository at this point in the history
As discussed in Ember core team meeting on 2015-10-23.
  • Loading branch information
rwjblue committed Oct 24, 2015
1 parent b0cad09 commit 20c70c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/ember-routing-htmlbars/lib/keywords/closure-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { symbol } from 'ember-metal/utils';
import { get } from 'ember-metal/property_get';
import { labelForSubexpr } from 'ember-htmlbars/hooks/subexpr';
import EmberError from 'ember-metal/error';
import run from 'ember-metal/run_loop';

export const INVOKE = symbol('INVOKE');
export const ACTION = symbol('ACTION');
Expand Down Expand Up @@ -70,25 +71,24 @@ function createClosureAction(target, action, valuePath, actionArguments) {
var closureAction;

if (actionArguments.length > 0) {
closureAction = function() {
closureAction = function(...passedArguments) {

This comment has been minimized.

Copy link
@stefanpenner

stefanpenner Oct 30, 2015

Member

why not just use run.bind

This comment has been minimized.

Copy link
@rwjblue

rwjblue Oct 30, 2015

Author Member

I believe either would work fine. Happy to take a peek at a PR changing it if you have the time.

var args = actionArguments;
if (arguments.length > 0) {
var passedArguments = Array.prototype.slice.apply(arguments);
if (passedArguments.length > 0) {
args = actionArguments.concat(passedArguments);
}
if (valuePath && args.length > 0) {
args[0] = get(args[0], valuePath);
}
return action.apply(target, args);

return run.join(target, action, ...args);
};
} else {
closureAction = function() {
var args = arguments;
closureAction = function(...args) {
if (valuePath && args.length > 0) {
args = Array.prototype.slice.apply(args);
args[0] = get(args[0], valuePath);
}
return action.apply(target, args);

return run.join(target, action, ...args);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,27 @@ QUnit.test('action closure does not get auto-mut wrapped', function(assert) {
innerComponent.fireAction();
});
});

QUnit.test('action should be called within a run loop', function(assert) {
assert.expect(1);

innerComponent = EmberComponent.extend({
fireAction() {
this.attrs.submit();
}
}).create();

outerComponent = EmberComponent.extend({
layout: compile(`{{view innerComponent submit=(action 'submit')}}`),
innerComponent,
actions: {
submit(newValue) {
assert.ok(run.currentRunLoop, 'action is called within a run loop');
}
}
}).create();

runAppend(outerComponent);

innerComponent.fireAction();
});

0 comments on commit 20c70c7

Please sign in to comment.