Permalink
Browse files

Initial stab at application reset

  • Loading branch information...
1 parent 2b50411 commit 47b1456e43a7c54b9b620409dcd0c15a0feb862f tomhuda committed Feb 9, 2013
View
1 .jshintrc
@@ -11,6 +11,7 @@
"requireModule",
"equal",
"notEqual",
+ "notStrictEqual",
"test",
"asyncTest",
"testBoth",
View
17 packages/ember-application/lib/system/application.js
@@ -443,6 +443,15 @@ var Application = Ember.Application = Ember.Namespace.extend(
return this;
},
+ reset: function() {
+ get(this, '__container__').destroy();
+ this.buildContainer();
+
+ this.isInitialized = false;
+ this.initialize();
+ this.startRouting();
+ },
+
/**
@private
@method runInitializers
@@ -530,6 +539,12 @@ var Application = Ember.Application = Ember.Namespace.extend(
router.startRouting();
},
+ handleURL: function(url) {
+ var router = this.__container__.lookup('router:main');
+
+ router.handleURL(url);
+ },
+
/**
Called when the Application has become ready.
The call will be delayed until the DOM has become ready.
@@ -544,7 +559,7 @@ var Application = Ember.Application = Ember.Namespace.extend(
var eventDispatcher = get(this, 'eventDispatcher');
if (eventDispatcher) { eventDispatcher.destroy(); }
- this.__container__.destroy();
+ get(this, '__container__').destroy();
},
initializer: function(options) {
View
98 packages/ember-application/tests/system/reset_test.js
@@ -0,0 +1,98 @@
+var get = Ember.get, set = Ember.set;
+
+var application;
+var Application;
+
+Application = Ember.Application.extend({
+ name: "App",
+ rootElement: "#qunit-fixture"
+});
+
+module("Ember.Application - resetting", {
+ teardown: function() {
+ if (application) {
+ Ember.run(function() {
+ application.destroy();
+ });
+ }
+ }
+});
+
+test("When an application is reset, new instances of controllers are generated", function() {
+ Ember.run(function() {
+ application = Application.create();
+ application.AcademicController = Ember.Controller.extend();
+ });
+
+ var firstController = application.__container__.lookup('controller:academic');
+ var secondController = application.__container__.lookup('controller:academic');
+
+ Ember.run(function() {
+ application.reset();
+ });
+
+ var thirdController = application.__container__.lookup('controller:academic');
+
+ strictEqual(firstController, secondController, "controllers looked up in succession should be the same instance");
+
+ ok(firstController.isDestroying, 'controllers are destroyed when their application is reset');
+
+ notStrictEqual(firstController, thirdController, "controllers looked up after the application is reset should not be the same instance");
+});
+
+test("When an application is reset, the ApplicationView is torn down", function() {
+ Ember.run(function() {
+ application = Application.create();
+ application.ApplicationView = Ember.View.extend({
+ elementId: "application-view"
+ });
+ });
+
+ equal(Ember.$('#qunit-fixture #application-view').length, 1, "precond - the application view is rendered");
+
+ var originalView = Ember.View.views['application-view'];
+
+ Ember.run(function() {
+ application.reset();
+ });
+
+ var resettedView = Ember.View.views['application-view'];
+
+ equal(Ember.$('#qunit-fixture #application-view').length, 1, "the application view is rendered");
+
+ notStrictEqual(originalView, resettedView, "The view object has changed");
+});
+
+test("When an application is reset, the router URL is reset to `/`", function() {
+ var location;
+
+ Ember.run(function() {
+ application = Application.create();
+ application.Router = Ember.Router.extend({
+ location: 'none'
+ });
+
+ application.Router.map(function() {
+ this.route('one');
+ this.route('two');
+ });
+ });
+
+ var router = application.__container__.lookup('router:main');
+
+ location = router.get('location');
+ location.handleURL('/one');
+
+ Ember.run(function() {
+ application.reset();
+ });
+
+ var applicationController = application.__container__.lookup('controller:application');
+
+ equal(get(applicationController, 'currentPath'), "index");
+
+ location = application.__container__.lookup('router:main').get('location');
+ location.handleURL('/one');
+
+ equal(get(applicationController, 'currentPath'), "one");
+});
View
7 packages/ember-routing/lib/location/none_location.js
@@ -27,7 +27,12 @@ Ember.NoneLocation = Ember.Object.extend({
},
onUpdateURL: function(callback) {
- // We are not wired up to the browser, so we'll never trigger the callback.
+ this.updateCallback = callback;
+ },
+
+ handleURL: function(url) {
+ set(this, 'path', url);
+ this.updateCallback(url);
},
formatURL: function(url) {
View
7 packages/ember-routing/lib/system/router.js
@@ -42,6 +42,10 @@ Ember.Router = Ember.Object.extend({
setupLocation(this);
},
+ url: Ember.computed(function() {
+ return get(this, 'location').getURL();
+ }),
+
startRouting: function() {
this.router = this.router || this.constructor.map(Ember.K);
@@ -55,10 +59,11 @@ Ember.Router = Ember.Object.extend({
container.register('view', 'default', DefaultView);
container.register('view', 'toplevel', Ember.View.extend());
- this.handleURL(location.getURL());
location.onUpdateURL(function(url) {
self.handleURL(url);
});
+
+ this.handleURL(location.getURL());
},
didTransition: function(infos) {

0 comments on commit 47b1456

Please sign in to comment.