From 1a71a8298c38a243d73c3dc55e1fbe75f0a07778 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Fri, 5 Feb 2016 10:39:14 -0500 Subject: [PATCH] Make functions returned "real" actions. 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). --- addon/-private/internals.js | 5 +++++ addon/helpers/route-action.js | 7 ++++++- tests/acceptance/main-test.js | 25 +++++++++++++++++++++++++ tests/dummy/app/router.js | 1 + tests/helpers/module-for-acceptance.js | 7 +++++++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 addon/-private/internals.js diff --git a/addon/-private/internals.js b/addon/-private/internals.js new file mode 100644 index 0000000..2e80dcb --- /dev/null +++ b/addon/-private/internals.js @@ -0,0 +1,5 @@ +import Ember from 'ember'; + +const ClosureActionModule = Ember.__loader.require('ember-routing-htmlbars/keywords/closure-action'); + +export const ACTION = ClosureActionModule.ACTION; diff --git a/addon/helpers/route-action.js b/addon/helpers/route-action.js index fb8e7fe..05a5764 100644 --- a/addon/helpers/route-action.js +++ b/addon/helpers/route-action.js @@ -1,5 +1,6 @@ import Ember from 'ember'; import getOwner from 'ember-getowner-polyfill'; +import { ACTION } from '../-private/internals'; const { A: emberArray, @@ -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; } }); diff --git a/tests/acceptance/main-test.js b/tests/acceptance/main-test.js index f2fae27..8ed91f1 100644 --- a/tests/acceptance/main-test.js +++ b/tests/acceptance/main-test.js @@ -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'); @@ -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``); + this.register('component:child-component', Component.extend({ + click() { + this.attrs.go(); + } + })); + + visit('/dynamic'); + click('.do-it'); +}); diff --git a/tests/dummy/app/router.js b/tests/dummy/app/router.js index 4745ee8..d3d52c1 100644 --- a/tests/dummy/app/router.js +++ b/tests/dummy/app/router.js @@ -9,6 +9,7 @@ Router.map(function() { this.route('thing', function() { this.route('show'); }); + this.route('dynamic'); }); export default Router; diff --git a/tests/helpers/module-for-acceptance.js b/tests/helpers/module-for-acceptance.js index ed23003..562c95a 100644 --- a/tests/helpers/module-for-acceptance.js +++ b/tests/helpers/module-for-acceptance.js @@ -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() {