I am trying to get my head around how to better structure my angularjs applications by not overloading my controllers with too much logic. I feel like I have read just about every article on the subject but was hoping someone might be able to provide advice based on a specific example. I have put the requirements below for a job listing (as in jobs for a service company) screen and the functionality required.
Requirements:
- Jobs are listed in a table and contain the following columns: id, customer, value ($), due date
- Data is coming from a backend api (laravel/php) which provides a json array of objects with nested related objects and array of objects. Data is raw e.g. value ($) is 123.23 and due date is 2015-03-09.
- The screen contains several filters for selecting a customer from a drop down list/autocomplete or selecting a date range for due date. Filtering is done server side, and server expects query params with a filter object e.g. filters={due_date_from='2015-03-07', due_date_to='2015-03-09',customer_id=123}
- Filters are only applied when the user clicks on a filter button
- Each column is sortable and sorting is done server side, so server expects query params with a sort object e.g. sort=[{by:'due_date', dir:'asc'}, {by:'customer_id',dir:'desc'}]
- 25 jobs per page, paging is done server side e.g. page=2
- Where jobs are overdue, the row should be red
- The state of filters, paging, sorts should be stored in the URL and read upon initialisation.
- A reset button should clear all filters
I am using the Restangular 3rd party module for pulling data from the server side and have used the extendModel methods to added helper functions such as isOverdue() which returns true/false.
Currently I have created the following properties and methods in my JobsListController.
$scope.jobs = [];
$scope.filters = [];
$scope.sorts = [];
$scope.loadData = function(){}; //build data packet to send to server
$scope.sort = function(); // checks if column is currently sort in order to reverse direction
$scope.reset = function(){}; //clears filters, sorts and paging to send to server
In my html I have the following things:
<input type="date" ng-model="filters.due_from" />
<td><a on-click="sortBy('customer_id')"></td>
Ultimately I think my question is, should I be creating a service/factory called JobsListModel rather than having all this functionality in the controller?