From bfee96c59eb42795179b7d6cc4dddc2f83a57f39 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 7 Sep 2014 09:30:48 +0200 Subject: [PATCH] Optimized sub routing in presenters --- public_html/js/Bootstrap.js | 2 +- .../js/Presenters/UserListPresenter.js | 32 +++++++++++-------- public_html/js/Router.js | 16 +++++----- public_html/js/Util.js | 18 ++++++++--- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/public_html/js/Bootstrap.js b/public_html/js/Bootstrap.js index 7d755845..643f9318 100644 --- a/public_html/js/Bootstrap.js +++ b/public_html/js/Bootstrap.js @@ -2,7 +2,7 @@ var App = App || {}; App.Bootstrap = function(auth, router, util, promise) { - util.initPresenter(function() { return App.DI.get('topNavigationPresenter'); }); + util.initPresenter('topNavigationPresenter'); promise.wait(auth.tryLoginFromCookie()) .then(startRouting) diff --git a/public_html/js/Presenters/UserListPresenter.js b/public_html/js/Presenters/UserListPresenter.js index 5c299034..9d00c6c8 100644 --- a/public_html/js/Presenters/UserListPresenter.js +++ b/public_html/js/Presenters/UserListPresenter.js @@ -18,26 +18,29 @@ App.Presenters.UserListPresenter = function( function init(args) { topNavigationPresenter.select('users'); - activeSearchOrder = util.parseComplexRouteArgs(args.searchArgs).order; promise.wait(util.promiseTemplate('user-list')).then(function(html) { template = _.template(html); - - pagedCollectionPresenter.init({ - searchArgs: args.searchArgs, - baseUri: '#/users', - backendUri: '/users', - renderCallback: function updateCollection(data) { - userList = data.entities; - render(); - }, - failCallback: function(response) { - $el.empty(); - messagePresenter.showError($el, response.json && response.json.error || response); - }}); + initPaginator(args); }); } + function initPaginator(args) { + activeSearchOrder = util.parseComplexRouteArgs(args.searchArgs).order; + pagedCollectionPresenter.init({ + searchArgs: args.searchArgs, + baseUri: '#/users', + backendUri: '/users', + renderCallback: function updateCollection(data) { + userList = data.entities; + render(); + }, + failCallback: function(response) { + $el.empty(); + messagePresenter.showError($el, response.json && response.json.error || response); + }}); + } + function render() { $el.html(template({ userList: userList, @@ -59,6 +62,7 @@ App.Presenters.UserListPresenter = function( return { init: init, + reinit: initPaginator, render: render }; diff --git a/public_html/js/Router.js b/public_html/js/Router.js index 3d843eff..04097ccf 100644 --- a/public_html/js/Router.js +++ b/public_html/js/Router.js @@ -19,12 +19,12 @@ App.Router = function(jQuery, util, appState) { }; function injectRoutes() { - inject('#/login', function() { return App.DI.get('loginPresenter'); }); - inject('#/logout', function() { return App.DI.get('logoutPresenter'); }); - inject('#/register', function() { return App.DI.get('registrationPresenter'); }); - inject('#/users', function() { return App.DI.get('userListPresenter'); }); - inject('#/users/:searchArgs', function() { return App.DI.get('userListPresenter'); }); - inject('#/user/:userName', function() { return App.DI.get('userPresenter'); }); + inject('#/login', 'loginPresenter'); + inject('#/logout', 'logoutPresenter'); + inject('#/register', 'registrationPresenter'); + inject('#/users', 'userListPresenter'); + inject('#/users/:searchArgs', 'userListPresenter'); + inject('#/user/:userName', 'userPresenter'); setRoot('#/users'); }; @@ -33,9 +33,9 @@ App.Router = function(jQuery, util, appState) { Path.root(newRoot); }; - function inject(path, presenterGetter) { + function inject(path, presenterName) { Path.map(path).to(function() { - util.initContentPresenter(presenterGetter, this.params); + util.initContentPresenter(presenterName, this.params); }); }; diff --git a/public_html/js/Util.js b/public_html/js/Util.js index 20840e5b..9127554d 100644 --- a/public_html/js/Util.js +++ b/public_html/js/Util.js @@ -3,6 +3,8 @@ var App = App || {}; App.Util = (function(jQuery, promise) { var templateCache = {}; + var lastContentPresenterName; + var lastContentPresenter; function parseComplexRouteArgs(args) { var result = {}; @@ -28,14 +30,20 @@ App.Util = (function(jQuery, promise) { return result; } - function initPresenter(presenterGetter, args) { - var presenter = presenterGetter(); + function initPresenter(presenterName, args) { + var presenter = App.DI.get(presenterName); presenter.init.call(presenter, args); } - function initContentPresenter(presenterGetter, args) { - //jQuery('#content').empty(); - initPresenter(presenterGetter, args); + function initContentPresenter(presenterName, args) { + if (lastContentPresenterName != presenterName) { + var presenter = App.DI.get(presenterName); + var initResult = presenter.init.call(presenter, args); + lastContentPresenterName = presenterName; + lastContentPresenter = presenter; + } else { + lastContentPresenter.reinit.call(presenter, args); + } }; function promiseTemplate(templateName) {