Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add autoStartRouting property to Ember.Application #1222

Closed
wants to merge 2 commits into from

5 participants

Joe West Don't Add Me To Your Organization a.k.a The Travis Bot Damien Mathieu Ray Cohen Peter Wagenet
Joe West

If router exists, flags whether startRouting is called automatically on App.initialize(). Defaults to true.

This is particularly useful for cases where you want the app to initialize but not start url routing immediately (such as determining whether a user has a valid session).

Joe West joewest Add autoStartRouting flag to Ember.Application
If `router` exists, determines whether startRouting is called automatically on
App.initialize(). Defaults to true
7df6568
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 7df6568 into d7e24ac).

Damien Mathieu

It would be very nice if you could also write tests for this.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged ff9abab into d7e24ac).

Ray Cohen

location appears to only be used inside startRouting

Ray Cohen

@joewest I'm just starting to use the router... what's the main benefit of doing this over determining user session prior to calling App.initialize()?

For example I'm doing something like

App.User.setupCurrentUser().always(function() {
  App.initialize();
})
Joe West

@raycohen That works too, tho you have to manually manage any controllers used in the bootstrap process since they're created by App.initialize(). My thinking was that it let's you use the router/state-manager to manage the loading or authentication process. So you have states like:

App.Router = Ember.Router.extend({
  root: Ember.Route.extend({
    loading: Em.State.extend(),
    isLoaded: Em.Route.extend(),
    unLoaded: Em.State.extend()
  })
});

Then inside of isLoaded you run App.startRouting(router) during enter/setup/connectOutlets.

Peter Wagenet
Owner

We now have an autoinit property for the application. I think that serves the same general function. Let me know if I'm mistaken.

Peter Wagenet wagenet closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 30, 2012
  1. Joe West

    Add autoStartRouting flag to Ember.Application

    joewest authored
    If `router` exists, determines whether startRouting is called automatically on
    App.initialize(). Defaults to true
Commits on Jul 31, 2012
  1. Joe West
This page is out of date. Refresh to see the latest.
41 packages/ember-application/lib/system/application.js
View
@@ -58,6 +58,16 @@ Ember.Application = Ember.Namespace.extend(
*/
customEvents: null,
+ /**
+ Determines whether URL routing should be started automatically (if
+ this Ember.Application has a router instance). Defaults to true, if set to
+ false you must manually call App.startRouting() to enable URL routing.
+
+ @type Boolean
+ @default true
+ */
+ autoStartRouting: true,
+
/** @private */
init: function() {
var eventDispatcher,
@@ -132,7 +142,23 @@ Ember.Application = Ember.Namespace.extend(
});
if (router && router instanceof Ember.Router) {
- this.startRouting(router);
+ var location = get(router, 'location'),
+ autoStartRouting = get(this, 'autoStartRouting'),
+ rootElement = get(this, 'rootElement'),
+ applicationController = get(router, 'applicationController');
+
+ Ember.assert("ApplicationView and ApplicationController must be defined on your application", (this.ApplicationView && applicationController) );
+
+ var applicationView = this.ApplicationView.create({
+ controller: applicationController
+ });
+ this._createdApplicationView = applicationView;
+
+ applicationView.appendTo(rootElement);
+
+ if (autoStartRouting) {
+ this.startRouting(router);
+ }
}
},
@@ -153,18 +179,7 @@ Ember.Application = Ember.Namespace.extend(
trigger a new call to `route` whenever the URL changes.
*/
startRouting: function(router) {
- var location = get(router, 'location'),
- rootElement = get(this, 'rootElement'),
- applicationController = get(router, 'applicationController');
-
- Ember.assert("ApplicationView and ApplicationController must be defined on your application", (this.ApplicationView && applicationController) );
-
- var applicationView = this.ApplicationView.create({
- controller: applicationController
- });
- this._createdApplicationView = applicationView;
-
- applicationView.appendTo(rootElement);
+ var location = get(router, 'location');
router.route(location.getURL());
location.onUpdateURL(function(url) {
38 packages/ember-application/tests/system/application_test.js
View
@@ -212,6 +212,44 @@ test("initialize application with stateManager via initialize call from Router c
equal(app.get('router.currentState.path'), 'root.index', "The router moved the state into the right place");
});
+test("initialize application with a router but start url routing manually", function() {
+ Ember.run(function() {
+ app = Ember.Application.create({
+ rootElement: '#qunit-fixture',
+ autoStartRouting: false
+ });
+
+ app.Router = Ember.Router.extend({
+ location: 'none',
+
+ root: Ember.Route.extend({
+ initialState: 'loading',
+
+ index: Ember.Route.extend({
+ route: '/'
+ }),
+
+ loading: Ember.State.extend()
+ })
+ });
+
+ app.ApplicationView = Ember.View.extend({
+ template: function() { return "Hello!"; }
+ });
+
+ app.ApplicationController = Ember.Controller.extend();
+
+ app.initialize();
+ });
+
+ equal(app.get('router.currentState.path'), 'root.loading', "The router moved to its initial state");
+
+ app.startRouting(app.router);
+
+ equal(app.get('router.currentState.path'), 'root.index', "Router moved to the correct state after url routing was started manually");
+
+});
+
test("injections can be registered in a specified order", function() {
var oldInjections = Ember.Application.injections;
Something went wrong with that request. Please try again.