Skip to content

Instantly share code, notes, and snippets.

@srph
Created October 26, 2014 19:11
Show Gist options
  • Save srph/60df7c5ec6139fbe0d6f to your computer and use it in GitHub Desktop.
Save srph/60df7c5ec6139fbe0d6f to your computer and use it in GitHub Desktop.
[AngularJS Controller Inheritance]: "this" not pointing to the child controller
+(function(angular, undefined) {
function ScholarshipsCtrl($scope, $state, $controller, Scholarship, UtilService, SCHOLARSHIP_COLUMNS) {
// Initialize variables
var vm = this,
controller,
// Assigns this variable to
// the data assigned by the resolve to the service
collection = [],
columns,
storage,
storageProperties;
angular.extend(collection, Scholarship.collection);
// Inherit BaseIndexCtrl's prototype
controller = $controller('BaseIndexCtrl', {
$scope: $scope,
UtilService: UtilService
});
angular.extend(this, controller);
// this.prototype = Object.create(controller.prototype);
// Column properties
storageProperties = {
scope: vm,
property: 'columns',
value: SCHOLARSHIP_COLUMNS,
key: 'scholarships'
};
// Initializes localStorage instance
// Creates a localStorage instance, then
// binds a localStorage property
// with the "column variable in the controller"
storage = UtilService.Storage(storageProperties);
columns = storage.provide();
// Initialize data
vm.columns = columns;
vm.collection = collection;
vm.remove = remove;
vm.callServer = callServer;
/**
* @description Sends a DELETE to the server
* @see getPageData
* @return void
*/
function remove() {
var selected = vm.getSelected();
}
function callServer(tableState) {
vm.load()
var query = {};
if (tableState.sort.predicate) {
query.sort = tableState.sort.reverse ? '' : '-';
query.sort += tableState.sort.predicate;
}
if (tableState.search.predicateObject) {
query.filters = {};
angular.forEach(tableState.search.predicateObject, function(value, key) {
this[key] = value;
}, query.filters);
}
getPageData(query);
}
/**
* Fetches data and replaces the current
* @param string query
* @return void
*/
function getPageData(query) {
Scholarship
.Query
.all(query)
.then(vm.applyResponse)
.catch(vm.catchPageDataError);
}
}
angular
.module('app')
.controller('ScholarshipsCtrl', ScholarshipsCtrl);
})(angular);
+(function(angular, _, undefined) {
//
// Serves as model / base for "index" controllers
// (for tables or listing). This eliminates redundant
// boilerplate code.
//
function BaseIndexCtrl($scope, UtilService) {
var vm = this,
columns = [],
collection = {},
storageProperties = {};
vm.applyPageData = applyPageData;
vm.catchPageDataError = catchPageDataError;
vm.collection = collection;
vm.columns = columns;
vm.isLoading = false;
vm.getSelected = getSelected;
vm.getPage = getPage;
vm.getPageData = getPageData;
vm.load = load;
vm.onColumnChange = onColumnChange;
/**
* @name applyPageData
* @description Callback function for promises after
* a server | ajax call
* @see getPageData
* @param {[type]} response [description]
* @return {[type]} [description]
*/
function applyPageData(response) {
vm.collection = response.data;
vm.load(false);
}
/**
* @name catchPageDataError
* @description Callback function for promises
* if a promise is not resolved
* @return {[type]} [description]
*/
function catchPageDataError() {
vm.load();
}
/**
* @name load
* @description Sets the load variable
* @return void
*/
function load(bool) {
vm.isLoading = bool || true;
}
/**
* @description Overrides the storage instance's
* value when the column settings are changed
* @return void
*/
function onColumnChange() {
storage.set(vm.columns);
}
/**
* @name getPage
* @description Runs tasks, then gets pageData.
* @param string dir previous|next
* @return void
*/
function getPage(dir) {
vm.load();
console.log(collection);
params = UtilService.decodeQuery(
vm.collection.paging[dir]
);
getPageData(params);
}
/**
* @name getPageData
* @description Fetches data and replaces the current
* @param string query
* @return void
*/
function getPageData(query) {
// ScholarshipSrvc
// .Query
// .all(query)
// .then(function(response) {
// loading();
// applyResponse(response);
// })
// .catch(function() {
// loading(false);
// });
}
/**
* @name getIDSelected
* @description Plucks the ids of each object
* having the isSelected property as true
* @return {Array}
*/
function getSelected(property) {
property || (property = 'id');
return _.chain()
.where('isSelected')
.pluck(property)
.value();
}
}
angular
.module('app')
.controller('BaseIndexCtrl', BaseIndexCtrl);
})(angular, _);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment