Skip to content

Commit

Permalink
Merge pull request #14805 from scalvert/router-service
Browse files Browse the repository at this point in the history
Public Router service MVP
  • Loading branch information
rwjblue committed Jan 20, 2017
2 parents ca76dba + 7daa911 commit 9cbf9f3
Show file tree
Hide file tree
Showing 7 changed files with 590 additions and 1 deletion.
3 changes: 2 additions & 1 deletion features.json
Expand Up @@ -7,6 +7,7 @@
"ember-glimmer-allow-backtracking-rerender": null,
"ember-testing-resume-test": null,
"ember-factory-for": true,
"ember-no-double-extend": null
"ember-no-double-extend": null,
"ember-routing-router-service": null
}
}
7 changes: 7 additions & 0 deletions packages/ember-application/lib/system/application.js
Expand Up @@ -35,6 +35,8 @@ import ApplicationInstance from './application-instance';
import { privatize as P } from 'container';
import Engine from './engine';
import { setupApplicationRegistry } from 'ember-glimmer';
import { RouterService } from 'ember-routing';
import { isFeatureEnabled } from 'ember-metal';

let librariesRegistered = false;

Expand Down Expand Up @@ -1038,6 +1040,11 @@ function commonSetupRegistry(registry) {
registry.register('location:none', NoneLocation);

registry.register(P`-bucket-cache:main`, BucketCache);

if (isFeatureEnabled('ember-routing-router-service')) {
registry.register('service:router', RouterService);
registry.injection('service:router', 'router', 'router:main');
}
}

function registerLibraries() {
Expand Down
1 change: 1 addition & 0 deletions packages/ember-routing/lib/index.js
Expand Up @@ -23,4 +23,5 @@ export { default as Router } from './system/router';
export { default as Route } from './system/route';
export { default as QueryParams } from './system/query_params';
export { default as RoutingService } from './services/routing';
export { default as RouterService } from './services/router';
export { default as BucketCache } from './system/cache';
49 changes: 49 additions & 0 deletions packages/ember-routing/lib/services/router.js
@@ -0,0 +1,49 @@
/**
@module ember
@submodule ember-routing
*/

import {
Service,
readOnly
} from 'ember-runtime';
import { get } from 'ember-metal';
import RouterDSL from '../system/dsl';

/**
The Router service is the public API that provides component/view layer
access to the router.
@public
@class RouterService
@category ember-routing-router-service
*/
const RouterService = Service.extend({
currentRouteName: readOnly('router.currentRouteName'),
currentURL: readOnly('router.currentURL'),
location: readOnly('router.location'),
rootURL: readOnly('router.rootURL'),

/**
Transition the application into another route. The route may
be either a single route or route path:
See [Route.transitionTo](http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo) for more info.
@method transitionTo
@category ember-routing-router-service
@param {String} name the name of the route or a URL
@param {...Object} models the model(s) or identifier(s) to be used while
transitioning to the route.
@param {Object} [options] optional hash with a queryParams property
containing a mapping of query parameters
@return {Transition} the transition object associated with this
attempted transition
@public
*/
transitionTo() {
this.router.transitionTo(...arguments);
}
});

export default RouterService;
8 changes: 8 additions & 0 deletions packages/ember-routing/lib/system/router.js
Expand Up @@ -136,6 +136,10 @@ const EmberRouter = EmberObject.extend(Evented, {
init() {
this._super(...arguments);

this.currentURL = null;
this.currentRouteName = null;
this.currentPath = null;

this._qpCache = new EmptyObject();
this._resetQueuedQueryParameterChanges();
this._handledErrors = dictionary(null);
Expand Down Expand Up @@ -627,6 +631,7 @@ const EmberRouter = EmberObject.extend(Evented, {

let doUpdateURL = () => {
location.setURL(lastURL);
set(emberRouter, 'currentURL', lastURL);
};

router.updateURL = path => {
Expand All @@ -637,6 +642,7 @@ const EmberRouter = EmberObject.extend(Evented, {
if (location.replaceURL) {
let doReplaceURL = () => {
location.replaceURL(lastURL);
set(emberRouter, 'currentURL', lastURL);
};

router.replaceURL = path => {
Expand Down Expand Up @@ -1290,9 +1296,11 @@ function updatePaths(router) {

let path = EmberRouter._routePath(infos);
let currentRouteName = infos[infos.length - 1].name;
let currentURL = router.get('location').getURL();

set(router, 'currentPath', path);
set(router, 'currentRouteName', currentRouteName);
set(router, 'currentURL', currentURL);

let appController = getOwner(router).lookup('controller:application');

Expand Down

0 comments on commit 9cbf9f3

Please sign in to comment.