Hello, I’m struggling a bit with deciding where some of my application logic should go (perhaps it doesn’t matter and is just a stylistic thing), and am wondering what other people do.
An example would be a paged listing of customers. One method of accomplishing this would be to have some pretty extensive routing code:
CustomersListController = RouteController.extend({
template: 'customers',
recordsPerPage: parseInt(Meteor.settings.public.recordsPerPage),
currentPage: function() {
return parseInt(this.params.page) || 1;
},
subscriptions: function() {
var skipCount = (this.currentPage() - 1) * this.recordsPerPage;
this.customersSub = Meteor.subscribe('customers',
skipCount, CustomerSortSettings.sortField(), CustomerSortSettings.sortDirection());
},
customers: function() {
var sortParams = {};
sortParams[CustomerSortSettings.sortField()] = CustomerSortSettings.sortDirection();
return Customers.find({}, { sort: sortParams });
},
prevPageLink: function() {
var prevPage = this.currentPage() === 1 ? 1 : this.currentPage() - 1;
return Router.routes.customers.path({page: prevPage});
},
nextPageLink: function() {
var totalCustomers = Counts.get('customerCount');
var nextPage = this.currentPage() * this.recordsPerPage < totalCustomers ?
this.currentPage() + 1 : this.currentPage();
return Router.routes.customers.path({page: nextPage});
},
data: function() {
return {
customers: this.customers(),
ready: this.customersSub.ready,
prevPage: this.prevPageLink(),
nextPage: this.nextPageLink()
};
}
});
Putting a bunch of logic in the route feels wrong to me, but maybe this is just because I come from a more traditional MVC background where route code tends to be pretty sparse.
The other option is to put a lot of this code in a template helper
var currentPage = function() {
return parseInt(Router.current().params.page) || 1;
}
Template.customers.helpers({
prevPage: function() {
var pp = currentPage() === 1 ? 1 : currentPage() - 1;
return Router.routes.customers.path({page: pp});
},
nextPage: function() {
var totalCustomers = Counts.get('customerCount');
var nextPage = currentPage() * parseInt(Meteor.settings.public.recordsPerPage)
< totalCustomers ? currentPage() + 1 : currentPage();
return Router.routes.customers.path({page: nextPage});
},
customers: function() {
var sortParams = {};
sortParams[CustomerSortSettings.sortField()] = CustomerSortSettings.sortDirection();
return Customers.find({}, { sort: sortParams });
},
});
Any thoughts on the best approach?