Optimized sub routing in presenters
This commit is contained in:
parent
73214396ce
commit
bfee96c59e
4 changed files with 40 additions and 28 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue