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

[CLEANUP beta] Store actions in actions not _actions. #12028

Merged
merged 1 commit into from
Aug 9, 2015
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export default function closureAction(morph, env, scope, params, hash, template,
}
if (target.actions) {
action = target.actions[actionName];
} else if (target._actions) {
action = target._actions[actionName];
}

if (!action) {
Expand Down
20 changes: 6 additions & 14 deletions packages/ember-routing/lib/system/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from 'ember-runtime/system/string';
import EmberObject from 'ember-runtime/system/object';
import Evented from 'ember-runtime/mixins/evented';
import ActionHandler from 'ember-runtime/mixins/action_handler';
import ActionHandler, { deprecateUnderscoreActions } from 'ember-runtime/mixins/action_handler';
import generateController from 'ember-routing/system/generate_controller';
import {
generateControllerFactory
Expand Down Expand Up @@ -308,7 +308,7 @@ var Route = EmberObject.extend(ActionHandler, Evented, {
router._updatingQPChanged(qp.urlKey);
},

mergedProperties: ['events', 'queryParams'],
mergedProperties: ['queryParams'],

/**
Retrieves parameters, for current route using the state.params
Expand Down Expand Up @@ -743,7 +743,7 @@ var Route = EmberObject.extend(ActionHandler, Evented, {
@private
*/

_actions: {
actions: {

queryParamsDidChange(changed, totalPresent, removed) {
var qpMap = get(this, '_qp').map;
Expand Down Expand Up @@ -844,15 +844,6 @@ var Route = EmberObject.extend(ActionHandler, Evented, {
}
},

/**
@deprecated

Please use `actions` instead.
@method events
@private
*/
events: null,

/**
This hook is executed when the router completely exits this route. It is
not executed when the model for the route changes.
Expand Down Expand Up @@ -1155,9 +1146,9 @@ var Route = EmberObject.extend(ActionHandler, Evented, {
} else {
var name = args[0];
args = slice.call(args, 1);
var action = this._actions[name];
var action = this.actions[name];
if (action) {
return this._actions[name].apply(this, args);
return this.actions[name].apply(this, args);
}
}
},
Expand Down Expand Up @@ -2069,6 +2060,7 @@ var Route = EmberObject.extend(ActionHandler, Evented, {
}
});

deprecateUnderscoreActions(Route);

Route.reopenClass({
isRouteFactory: true
Expand Down
4 changes: 2 additions & 2 deletions packages/ember-routing/lib/system/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,8 @@ function triggerEvent(handlerInfos, ignoreFailure, args) {
handlerInfo = handlerInfos[i];
handler = handlerInfo.handler;

if (handler._actions && handler._actions[name]) {
if (handler._actions[name].apply(handler, args) === true) {
if (handler.actions && handler.actions[name]) {
if (handler.actions[name].apply(handler, args) === true) {
eventWasHandled = true;
} else {
return;
Expand Down
16 changes: 16 additions & 0 deletions packages/ember-routing/tests/system/route_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ QUnit.test('.send just calls an action if the routers internal router property i
equal(undefined, route.send('nonexistent', 1, 2, 3));
});

QUnit.test('can access `actions` hash via `_actions` [DEPRECATED]', function() {
expect(2);

var route = EmberRoute.extend({
actions: {
foo: function() {
ok(true, 'called foo action');
}
}
}).create();

expectDeprecation(function() {
route._actions.foo();
}, 'Usage of `_actions` is deprecated, use `actions` instead.');
});

QUnit.module('Ember.Route serialize', {
setup: setup,
teardown: teardown
Expand Down
3 changes: 3 additions & 0 deletions packages/ember-runtime/lib/controllers/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Ember from 'ember-metal/core'; // Ember.assert
import EmberObject from 'ember-runtime/system/object';
import Mixin from 'ember-runtime/mixins/controller';
import { createInjectionHelper } from 'ember-runtime/inject';
import { deprecateUnderscoreActions } from 'ember-runtime/mixins/action_handler';

/**
@module ember
Expand All @@ -17,6 +18,8 @@ import { createInjectionHelper } from 'ember-runtime/inject';
*/
var Controller = EmberObject.extend(Mixin);

deprecateUnderscoreActions(Controller);

function controllerInjectionHelper(factory) {
Ember.assert('Defining an injected controller property on a ' +
'non-controller is not allowed.', Mixin.detect(factory.PrototypeMixin));
Expand Down
38 changes: 10 additions & 28 deletions packages/ember-runtime/lib/mixins/action_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
@submodule ember-runtime
*/
import Ember from 'ember-metal/core';
import merge from 'ember-metal/merge';
import { Mixin } from 'ember-metal/mixin';
import { get } from 'ember-metal/property_get';
import { deprecateProperty } from 'ember-metal/deprecate_property';

/**
The `Ember.ActionHandler` mixin implements support for moving an `actions`
property to an `_actions` property at extend time, and adding `_actions`
to the object's mergedProperties list.

`Ember.ActionHandler` is available on some familiar classes including
`Ember.Route`, `Ember.View`, `Ember.Component`, and `Ember.Controller`.
(Internally the mixin is used by `Ember.CoreView`, `Ember.ControllerMixin`,
Expand All @@ -23,7 +19,7 @@ import { get } from 'ember-metal/property_get';
@private
*/
var ActionHandler = Mixin.create({
mergedProperties: ['_actions'],
mergedProperties: ['actions'],

/**
The collection of functions, keyed by name, available on this
Expand Down Expand Up @@ -146,26 +142,6 @@ var ActionHandler = Mixin.create({
@public
*/

/**
Moves `actions` to `_actions` at extend time. Note that this currently
modifies the mixin themselves, which is technically dubious but
is practically of little consequence. This may change in the future.

@private
@method willMergeMixin
*/
willMergeMixin(props) {
if (!props._actions) {
Ember.assert('\'actions\' should not be a function', typeof(props.actions) !== 'function');

if (!!props.actions && typeof props.actions === 'object') {
let hashName = 'actions';
props._actions = merge(props._actions || {}, props[hashName]);
delete props[hashName];
}
}
},

/**
Triggers a named action on the `ActionHandler`. Any parameters
supplied after the `actionName` string will be passed as arguments
Expand Down Expand Up @@ -199,8 +175,8 @@ var ActionHandler = Mixin.create({
send(actionName, ...args) {
var target;

if (this._actions && this._actions[actionName]) {
var shouldBubble = this._actions[actionName].apply(this, args) === true;
if (this.actions && this.actions[actionName]) {
var shouldBubble = this.actions[actionName].apply(this, args) === true;
if (!shouldBubble) { return; }
}

Expand All @@ -213,3 +189,9 @@ var ActionHandler = Mixin.create({
});

export default ActionHandler;

export function deprecateUnderscoreActions(factory) {
deprecateProperty(factory.prototype, '_actions', 'actions', {
id: 'ember-runtime.action-handler-_actions', until: '3.0.0'
});
}
43 changes: 14 additions & 29 deletions packages/ember-runtime/tests/controllers/controller_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,35 @@ import { get } from 'ember-metal/property_get';

QUnit.module('Controller event handling');

QUnit.test('Action can be handled by a function on actions object', function() {
expect(1);
var TestController = Controller.extend({
QUnit.test('can access `actions` hash via `_actions` [DEPRECATED]', function() {
expect(2);

var controller = Controller.extend({
actions: {
poke() {
ok(true, 'poked');
foo: function() {
ok(true, 'called foo action');
}
}
});
var controller = TestController.create({});
controller.send('poke');
}).create();

expectDeprecation(function() {
controller._actions.foo();
}, 'Usage of `_actions` is deprecated, use `actions` instead.');
});

// TODO: Can we support this?
// QUnit.test("Actions handlers can be configured to use another name", function() {
// expect(1);
// var TestController = Controller.extend({
// actionsProperty: 'actionHandlers',
// actionHandlers: {
// poke: function() {
// ok(true, 'poked');
// }
// }
// });
// var controller = TestController.create({});
// controller.send("poke");
// });

QUnit.test('When `_actions` is provided, `actions` is left alone', function() {
expect(2);
QUnit.test('Action can be handled by a function on actions object', function() {
expect(1);
var TestController = Controller.extend({
actions: ['foo', 'bar'],
_actions: {
actions: {
poke() {
ok(true, 'poked');
}
}
});
var controller = TestController.create({});
controller.send('poke');
equal('foo', controller.get('actions')[0], 'actions property is not untouched');
});


QUnit.test('A handled action can be bubbled to the target for continued processing', function() {
expect(2);
var TestController = Controller.extend({
Expand Down
18 changes: 0 additions & 18 deletions packages/ember-runtime/tests/mixins/action_handler_test.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/ember-views/lib/views/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ var Component = View.extend(TargetActionSupport, {

send(actionName, ...args) {
var target;
var hasAction = this._actions && this._actions[actionName];
var hasAction = this.actions && this.actions[actionName];

if (hasAction) {
var shouldBubble = this._actions[actionName].apply(this, args) === true;
var shouldBubble = this.actions[actionName].apply(this, args) === true;
if (!shouldBubble) { return; }
}

Expand Down
4 changes: 3 additions & 1 deletion packages/ember-views/lib/views/core_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { get } from 'ember-metal/property_get';

import EmberObject from 'ember-runtime/system/object';
import Evented from 'ember-runtime/mixins/evented';
import ActionHandler from 'ember-runtime/mixins/action_handler';
import ActionHandler, { deprecateUnderscoreActions } from 'ember-runtime/mixins/action_handler';
import { typeOf } from 'ember-runtime/utils';

import Renderer from 'ember-metal-views/renderer';
Expand Down Expand Up @@ -129,6 +129,8 @@ var CoreView = EmberObject.extend(Evented, ActionHandler, {
destroyElement: K
});

deprecateUnderscoreActions(CoreView);

CoreView.reopenClass({
isViewFactory: true
});
Expand Down
16 changes: 16 additions & 0 deletions packages/ember-views/tests/views/component_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ QUnit.module('Ember.Component', {
}
});

QUnit.test('can access `actions` hash via `_actions` [DEPRECATED]', function() {
expect(2);

component = Component.extend({
actions: {
foo: function() {
ok(true, 'called foo action');
}
}
}).create();

expectDeprecation(function() {
component._actions.foo();
}, 'Usage of `_actions` is deprecated, use `actions` instead.');
});

QUnit.test('The context of an Ember.Component is itself', function() {
strictEqual(component, component.get('context'), 'A component\'s context is itself');
});
Expand Down