Skip to content

Commit

Permalink
Make functions returned "real" actions.
Browse files Browse the repository at this point in the history
This prevents the extra/double mutable object wrapping when passing down
from one layer to the next (and avoids requiring extra `(action`
wrapping to prevent this).
  • Loading branch information
rwjblue committed Feb 5, 2016
1 parent 6288557 commit 1a71a82
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions addon/-private/internals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Ember from 'ember';

const ClosureActionModule = Ember.__loader.require('ember-routing-htmlbars/keywords/closure-action');

export const ACTION = ClosureActionModule.ACTION;
7 changes: 6 additions & 1 deletion addon/helpers/route-action.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ember from 'ember';
import getOwner from 'ember-getowner-polyfill';
import { ACTION } from '../-private/internals';

const {
A: emberArray,
Expand Down Expand Up @@ -37,12 +38,16 @@ export default Helper.extend({
let router = get(this, 'router');
assert('[ember-route-action-helper] Unable to lookup router', router);

return function(...invocationArgs) {
let action = function(...invocationArgs) {
let args = params.concat(invocationArgs);
let { action, handler } = getRouteWithAction(router, actionName);
assert(`[ember-route-action-helper] Unable to find action ${actionName}`, handler);

return action.apply(handler, args);
};

action[ACTION] = true;

return action;
}
});
25 changes: 25 additions & 0 deletions tests/acceptance/main-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
import Ember from 'ember';
import hbs from 'htmlbars-inline-precompile';

const { Route, Component } = Ember;

moduleForAcceptance('Acceptance | main');

Expand Down Expand Up @@ -28,3 +32,24 @@ test('it has a return value', function(assert) {
andThen(() => assert.equal(findWithAssert('.thing-show .max-value').text().trim(), '300'));
});

test('it can be used without rewrapping with (action (route-action "foo"))', function(assert) {
this.register('route:dynamic', Route.extend({
actions: {
foo() {
assert.ok(true, 'action was properly triggered on the route');
}
}
}));

this.register('template:dynamic', hbs`{{parent-component go=(route-action 'foo') }}`);
this.register('template:components/parent-component', hbs`{{child-component go=go}}`);
this.register('template:components/child-component', hbs`<button class="do-it">GO!</button>`);
this.register('component:child-component', Component.extend({
click() {
this.attrs.go();
}
}));

visit('/dynamic');
click('.do-it');
});
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Router.map(function() {
this.route('thing', function() {
this.route('show');
});
this.route('dynamic');
});

export default Router;
7 changes: 7 additions & 0 deletions tests/helpers/module-for-acceptance.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export default function(name, options = {}) {
if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}

this.register = (fullName, Factory) => {
let instance = this.application.__deprecatedInstance__;
let registry = instance.register ? instance : instance.registry;

return registry.register(fullName, Factory);
};
},

afterEach() {
Expand Down

0 comments on commit 1a71a82

Please sign in to comment.