Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active January 6, 2016 05:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhawkins/efe10bdfbed2aae46ab1 to your computer and use it in GitHub Desktop.
Save alexhawkins/efe10bdfbed2aae46ab1 to your computer and use it in GitHub Desktop.
Sample Controller and Service using ES6 and John Papa Style Guide
(() => {
'use strict';
/*************************************************************
* @ngdoc controller
* @name dashboard.customer.controller:CustomerCtrl
*
* @description
*
* CustomerCtrl Class for Customer Model
*************************************************************/
angular
.module('dashboard.customer')
.controller('CustomerCtrl', CustomerCtrl);
CustomerCtrl.$inject = ['DashboardCustomers', '$log', '$q', '$scope', '$mdToast'];
function CustomerCtrl(DashboardCustomers, $log, $q, $scope, $mdToast) {
var availableCustomers = [];
const vm = this;
vm.customers = availableCustomers;
vm.selectedCustomer = null;
vm.orderHeaders = [];
vm.isLoadingCustomers = false;
vm.isLoadingOrders = false;
vm.select = selectCustomer;
vm.isSelected = isSelected;
vm.filterCriteria = '';
vm.filteredByName = filteredByName;
vm.openToast = openToast;
vm.contentIsLoaded = false;
vm.limit = 10;
// Perform Auto-load on all known customers
showLoading()
.then(activateCustomer)
.then(getCustomerOrders)
.then(DashboardCustomers.getCustomers)
.then(showCustomers)
.then(enableAutoSelect)
.finally(hideLoading);
/***********************************************************
/* Private Methods
/***********************************************************/
function isSelected(customer) {
return vm.selectedCustomer === customer;
}
/**
* Internally watch the filterCriteria for changes...
* then auto-select the customer if it is the ONLY one found...
*/
function enableAutoSelect() {
var unwatch = $scope.$watch(function() {
return vm.filterCriteria;
}, autoSelectCustomer);
// Make sure to `unwatch` when the controller is released...
$scope.$on('$destroy', unwatch);
}
/********************************************************************************/
/********************SERVICE EXAMPLE************************************************************************************/
(() => {
'use strict';
/*************************************************************
* @ngdoc service
* @name dashboard.customer.service:DashboardCustomers
*
* @description
*
* CustomerService Class provides a delegate layer with all
* and its `customer.*.html` sub-view templates
*************************************************************/
angular
.module('dashboard.customer')
.factory('DashboardCustomers', DashboardCustomers);
function DashboardCustomers($http, $q, $log) {
/* handle state in factory */
var orders = [];
var customers = [];
var service = {
getCustomers: getCustomers,
getOrdersFor: getOrdersFor
};
return service;
/**********************************************************
/* Get Customer Data
/**********************************************************/
function getCustomers() {
return $http.get('data/customers.json')
.then(cacheCustomers)
.catch(getCustomersFailed);
function cacheCustomers(response) {
customers = extract(response);
return customers;
}
function getCustomersFailed(error) {
$log.debug('XHR Failed for geCustomers.' + error.data);
}
}
/**********************************************************
/* Get Orders for Given Customer
/**********************************************************/
function getOrdersFor(customer) {
return !customer ?
$model.$q.reject("Invalid Customer `null`") :
$http.get('data/orders.json')
.then(cacheOrders)
.catch(getOrdersFailed);
function cacheOrders(response) {
orders = extract(response, true, customer);
return orders;
}
function getOrdersFailed(error) {
$log.debug('XHR Failed for getOrders.' + error.data);
}
}
/**********************************************************
/* Response Handlers
/**********************************************************/
function extract(response, where, customer) {
where = where || false;
return !where ?
_.sortByAll(response.data, ['firstName', 'lastName']) :
_.sortBy(_.where(response.data, { 'customerId': customer._id }), 'ordered');
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment