diff --git a/config/environment.js b/config/environment.js index c02e62c..bac1a05 100644 --- a/config/environment.js +++ b/config/environment.js @@ -15,7 +15,8 @@ module.exports = function(/* environment, appConfig */) { _ENABLE_DID_INIT_ATTRS_SUPPORT: true, _ENABLE_RENDER_SUPPORT: true, _ENABLE_REVERSED_OBSERVER_SUPPORT: true, - _ENABLE_INITIALIZER_ARGUMENTS_SUPPORT: true + _ENABLE_INITIALIZER_ARGUMENTS_SUPPORT: true, + _ENABLE_ROUTER_RESOURCE: true } }; }; diff --git a/index.js b/index.js index e4488f0..4228bcf 100644 --- a/index.js +++ b/index.js @@ -13,5 +13,6 @@ module.exports = { this.import('vendor/underscore-actions.js'); this.import('vendor/reversed-observer.js'); this.import('vendor/initializer-arity.js'); + this.import('vendor/router-resource.js'); } }; diff --git a/tests/unit/router-resource-test.js b/tests/unit/router-resource-test.js new file mode 100644 index 0000000..2858787 --- /dev/null +++ b/tests/unit/router-resource-test.js @@ -0,0 +1,75 @@ +import { module, test } from 'ember-qunit'; +import EmberRouter from '@ember/routing/router'; + +let Router; + +module('Ember.RouterDSL.resource', { + beforeEach() { + Router = EmberRouter.extend(); + }, + + afterEach() { + Router = null; + } +}); + +test('should reset namespace if nested with resource', function(assert) { + assert.expect(4); + + assert.expectDeprecation(() => { + Router = Router.map(function() { + this.resource('bleep', function() { + this.resource('bloop', function() { + this.resource('blork'); + }); + }); + }); + + let router = Router.create(); + router._initRouterJs(); + + if (router._routerMicrolib) { + assert.ok(router._routerMicrolib.recognizer.names['bleep'], 'nested resources do not contain parent name'); + assert.ok(router._routerMicrolib.recognizer.names['bloop'], 'nested resources do not contain parent name'); + assert.ok(router._routerMicrolib.recognizer.names['blork'], 'nested resources do not contain parent name'); + } + else { + assert.ok(router.router.recognizer.names['bleep'], 'nested resources do not contain parent name'); + assert.ok(router.router.recognizer.names['bloop'], 'nested resources do not contain parent name'); + assert.ok(router.router.recognizer.names['blork'], 'nested resources do not contain parent name'); + } + + }, 'this.resource() is deprecated. Use this.route(\'name\', { resetNamespace: true }, function () {}) instead.'); +}); + +test('should fail when using a reserved route name', function(assert) { + assert.expectDeprecation(() => { + let reservedNames = ['array', 'basic', 'object', 'application']; + + assert.expect((reservedNames.length * 2) + 1); + + reservedNames.forEach(reservedName => { + assert.expectAssertion(() => { + Router = EmberRouter.extend(); + + Router.map(function() { + this.route(reservedName); + }); + + let router = Router.create(); + router._initRouterJs(); + }, '\'' + reservedName + '\' cannot be used as a route name.'); + + assert.expectAssertion(() => { + Router = EmberRouter.extend(); + + Router.map(function() { + this.resource(reservedName); + }); + + let router = Router.create(); + router._initRouterJs(); + }, `'${reservedName}' cannot be used as a route name.`); + }); + }, 'this.resource() is deprecated. Use this.route(\'name\', { resetNamespace: true }, function () {}) instead.'); +}); diff --git a/vendor/router-resource.js b/vendor/router-resource.js new file mode 100644 index 0000000..fa12b01 --- /dev/null +++ b/vendor/router-resource.js @@ -0,0 +1,27 @@ +(function() { + var _Ember; + if (typeof Ember !== 'undefined') { + _Ember = Ember; + } else { + _Ember = require('ember').default; + } + + if (EmberENV && EmberENV._ENABLE_ROUTER_RESOURCE !== true) { + return false; + } + + _Ember.RouterDSL.prototype.resource = function(name, options, callback) { + if (arguments.length === 2 && typeof options === 'function') { + callback = options; + options = {}; + } + + if (typeof options === 'undefined') { + options = {}; + } + + options.resetNamespace = true; + _Ember.deprecate('this.resource() is deprecated. Use this.route(\'name\', { resetNamespace: true }, function () {}) instead.', false, { id: 'ember-routing.router-resource', until: '3.0.0' }); + this.route(name, options, callback); + } +})();