Skip to content

Commit

Permalink
Merge pull request emberjs#2563 from ef4/render-optimization
Browse files Browse the repository at this point in the history
Prevent unnecessary re-rendering when only route context has changed
  • Loading branch information
stefanpenner committed Apr 26, 2013
2 parents 68fc7fc + f67792e commit 082a987
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
13 changes: 12 additions & 1 deletion packages/ember-routing/lib/system/route.js
Expand Up @@ -198,15 +198,26 @@ Ember.Route = Ember.Object.extend({
} else {
this.setupController(controller, context);
}
},

/**
@private
This hook is an entry point for router.js. It is invoked when
we're entering a route, after the route's context has been setup.
@method setupTemplate
*/
setupTemplate: function(context) {
if (this.renderTemplates) {
Ember.deprecate("Ember.Route.renderTemplates is deprecated. Please use Ember.Route.renderTemplate(controller, model) instead.");
this.renderTemplates(context);
} else {
this.renderTemplate(controller, context);
this.renderTemplate(this.controller, context);
}
},


/**
A hook you can implement to optionally redirect to another route.
Expand Down
8 changes: 6 additions & 2 deletions packages/ember-routing/lib/vendor/router.js
Expand Up @@ -298,6 +298,7 @@ define("router",
if (handler) {
if (handler.enter) { handler.enter(); }
if (handler.setup) { handler.setup(); }
if (handler.setupTemplate) { handler.setupTemplate(); }
}
}
}
Expand Down Expand Up @@ -340,6 +341,7 @@ define("router",
if (handler){
if (handler.enter) { handler.enter(); }
if (handler.setup) { handler.setup(error); }
if (handler.setupTemplate) { handler.setupTemplate(error); }
}
}

Expand Down Expand Up @@ -495,10 +497,12 @@ define("router",
aborted = true;
}
}

if (!aborted) {
if (handler.setupTemplate) {
handler.setupTemplate(context);
}
currentHandlerInfos.push(handlerInfo);
}
}
});

if (!aborted && router.didTransition) {
Expand Down
48 changes: 48 additions & 0 deletions packages/ember/tests/routing/basic_test.js
Expand Up @@ -1773,3 +1773,51 @@ test("Child routes should render inside the application template if the applicat

equal(Ember.$('#qunit-fixture > div').text(), "App posts");
});

test("The template is not re-rendered when the route's context changes", function() {
Router.map(function() {
this.route("page", { path: "/page/:name" });
});

App.PageRoute = Ember.Route.extend({
model: function(params) {
return Ember.Object.create({name: params.name});
}
});

var insertionCount = 0;
App.PageView = Ember.View.extend({
didInsertElement: function() {
insertionCount += 1;
}
});

Ember.TEMPLATES.page = Ember.Handlebars.compile(
"<p>{{name}}</p>"
);

bootApplication();

Ember.run(function() {
router.handleURL("/page/first");
});

equal(Ember.$('p', '#qunit-fixture').text(), "first");
equal(insertionCount, 1);

Ember.run(function() {
router.handleURL("/page/second");
});

equal(Ember.$('p', '#qunit-fixture').text(), "second");
equal(insertionCount, 1, "view should have inserted only once");

Ember.run(function() {
router.transitionTo('page', Ember.Object.create({name: 'third'}));
});

equal(Ember.$('p', '#qunit-fixture').text(), "third");
equal(insertionCount, 1, "view should still have inserted only once");
});


0 comments on commit 082a987

Please sign in to comment.