Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make functions returned "real" actions. #8

Merged
merged 2 commits into from
Feb 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
12 changes: 9 additions & 3 deletions addon/helpers/route-action.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import Ember from 'ember';
import getOwner from 'ember-getowner-polyfill';
import { ACTION } from '../-private/internals';

const {
A: emberArray,
Helper,
assert,
computed,
typeOf,
get
get,
run
} = Ember;

function getRoutes(router) {
Expand Down Expand Up @@ -37,12 +39,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);
return run.join(handler, action, ...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