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

[REFACTOR] Lazily reify router’s location #10483

Merged
merged 3 commits into from
Mar 5, 2015
Merged

[REFACTOR] Lazily reify router’s location #10483

merged 3 commits into from
Mar 5, 2015

Conversation

tomdale
Copy link
Member

@tomdale tomdale commented Feb 17, 2015

Users can override a router’s location by specifying its location property as a string. For example, to change the router from the “auto” location to the “none” location, users can do the following:

// app/router.js
export default Ember.Router.extend({
 location: 'none'
});

Previously, the reification of the string value into a concrete Location implementation happened at router creation time.

This immediate reification made it difficult to make changes to the router configuration, since it had to be done at creation time. In general, classes that require configuration to be set at creation time are unwieldy to use with the container, since the container itself manages creation.

For example, in the case of the Application's visit() API, the application instance would like to override the router to use the none location.

When reification was at creation time, the router was created with the wrong location. Before the default could be overridden, the router would try to set up things like listeners on the browser's address bar, which would cause an exception to be thrown in Node environments where there is no notion of a URL.

In this commit, reifying and setting up the location are deferred until the last possible moment, when routing starts (either by calling startRouting(), which starts the app at the browser's current URL, or by calling handleURL(), which starts the app at a provided URL). This allows the application to detect if it is in autoboot mode or not, and override the router's location before routing begins.

@@ -103,8 +105,12 @@ export default EmberObject.extend({
this.registry.register('-application-instance:main', this, { instantiate: false });
},

router: computed(function() {
return this.container.lookup('router:main');
}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readOnly ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stefanpenner
Copy link
Member

although maybe out of scope, the pattern of overriting an attribute like this is crappy, can we move away from it?

@tomdale
Copy link
Member Author

tomdale commented Feb 24, 2015

@stefanpenner What do you mean overwriting an attribute?

@stefanpenner
Copy link
Member

The location property is rewritten from string to instance

@tomdale
Copy link
Member Author

tomdale commented Feb 25, 2015

@stefanpenner Yes, this pattern is terrible. Unfortunately it is used in almost every Ember app. We should not introduce new APIs that use this pattern but I think we're shackled with it for the time being.

@stefanpenner
Copy link
Member

@stefanpenner Yes, this pattern is terrible. Unfortunately it is used in almost every Ember app. We should not introduce new APIs that use this pattern but I think we're shackled with it for the time being.

when do people access routerInstance.location directly?

Users can override a router’s location by specifying its `location`
property as a string. For example, to change the router from the “auto”
location to the “none” location, users can do the following:

```js
// app/router.js
export default Ember.Router.extend({
 location: 'none'
});
```

Previously, the reification of the string value into a concrete Location
implementation happened at router creation time.

This immediate reification made it difficult to make changes to the
router configuration, since it had to be done at creation time. In
general, classes that require configuration to be set at creation time
are unwieldy to use with the container, since the container itself
manages creation.

For example, in the case of the Application's `visit()` API, the
application instance would like to override the router to use the
`none` location.

When reification was at creation time, the router was created with the
wrong location. Before the default could be overridden, the router would
try to set up things like listeners on the browser's address bar, which
would cause an exception to be thrown in Node environments where there
is no notion of a URL.

In this commit, reifying and setting up the location are deferred until
the last possible moment, when routing starts (either by calling
`startRouting()`, which starts the app at the browser's current URL, or
by calling `handleURL()`, which starts the app at a provided URL). This
allows the application to detect if it is in autoboot mode or not, and
override the router's location before routing begins.
tomdale added a commit that referenced this pull request Mar 5, 2015
[REFACTOR] Lazily reify router’s location
@tomdale tomdale merged commit c10362b into master Mar 5, 2015
@stefanpenner stefanpenner deleted the lazy-location branch March 5, 2015 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants