Optimized sub routing in presenters

This commit is contained in:
Marcin Kurczewski 2014-09-07 09:30:48 +02:00
parent 73214396ce
commit bfee96c59e
4 changed files with 40 additions and 28 deletions

View file

@ -2,7 +2,7 @@ var App = App || {};
App.Bootstrap = function(auth, router, util, promise) { App.Bootstrap = function(auth, router, util, promise) {
util.initPresenter(function() { return App.DI.get('topNavigationPresenter'); }); util.initPresenter('topNavigationPresenter');
promise.wait(auth.tryLoginFromCookie()) promise.wait(auth.tryLoginFromCookie())
.then(startRouting) .then(startRouting)

View file

@ -18,26 +18,29 @@ App.Presenters.UserListPresenter = function(
function init(args) { function init(args) {
topNavigationPresenter.select('users'); topNavigationPresenter.select('users');
activeSearchOrder = util.parseComplexRouteArgs(args.searchArgs).order;
promise.wait(util.promiseTemplate('user-list')).then(function(html) { promise.wait(util.promiseTemplate('user-list')).then(function(html) {
template = _.template(html); template = _.template(html);
initPaginator(args);
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 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() { function render() {
$el.html(template({ $el.html(template({
userList: userList, userList: userList,
@ -59,6 +62,7 @@ App.Presenters.UserListPresenter = function(
return { return {
init: init, init: init,
reinit: initPaginator,
render: render render: render
}; };

View file

@ -19,12 +19,12 @@ App.Router = function(jQuery, util, appState) {
}; };
function injectRoutes() { function injectRoutes() {
inject('#/login', function() { return App.DI.get('loginPresenter'); }); inject('#/login', 'loginPresenter');
inject('#/logout', function() { return App.DI.get('logoutPresenter'); }); inject('#/logout', 'logoutPresenter');
inject('#/register', function() { return App.DI.get('registrationPresenter'); }); inject('#/register', 'registrationPresenter');
inject('#/users', function() { return App.DI.get('userListPresenter'); }); inject('#/users', 'userListPresenter');
inject('#/users/:searchArgs', function() { return App.DI.get('userListPresenter'); }); inject('#/users/:searchArgs', 'userListPresenter');
inject('#/user/:userName', function() { return App.DI.get('userPresenter'); }); inject('#/user/:userName', 'userPresenter');
setRoot('#/users'); setRoot('#/users');
}; };
@ -33,9 +33,9 @@ App.Router = function(jQuery, util, appState) {
Path.root(newRoot); Path.root(newRoot);
}; };
function inject(path, presenterGetter) { function inject(path, presenterName) {
Path.map(path).to(function() { Path.map(path).to(function() {
util.initContentPresenter(presenterGetter, this.params); util.initContentPresenter(presenterName, this.params);
}); });
}; };

View file

@ -3,6 +3,8 @@ var App = App || {};
App.Util = (function(jQuery, promise) { App.Util = (function(jQuery, promise) {
var templateCache = {}; var templateCache = {};
var lastContentPresenterName;
var lastContentPresenter;
function parseComplexRouteArgs(args) { function parseComplexRouteArgs(args) {
var result = {}; var result = {};
@ -28,14 +30,20 @@ App.Util = (function(jQuery, promise) {
return result; return result;
} }
function initPresenter(presenterGetter, args) { function initPresenter(presenterName, args) {
var presenter = presenterGetter(); var presenter = App.DI.get(presenterName);
presenter.init.call(presenter, args); presenter.init.call(presenter, args);
} }
function initContentPresenter(presenterGetter, args) { function initContentPresenter(presenterName, args) {
//jQuery('#content').empty(); if (lastContentPresenterName != presenterName) {
initPresenter(presenterGetter, args); 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) { function promiseTemplate(templateName) {