Skip to content

Instantly share code, notes, and snippets.

@ptgamr
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptgamr/825868fc2d6378b686e4 to your computer and use it in GitHub Desktop.
Save ptgamr/825868fc2d6378b686e4 to your computer and use it in GitHub Desktop.
AngularJS Controller Inheritance
/**
* Base controller for all controllers.
* Use this as a template for all future controllers
*
* Use of Class.js
*/
var BaseController = Class.extend({
$scope:null,
/**
* Initialize Notes Controller
* @param $scope, current controller scope
*/
init:function(scope){
this.$scope = scope;
this.defineListeners();
this.defineScope();
},
/**
* Initialize listeners needs to be overrided by the subclass.
* Don't forget to call _super() to activate
*/
defineListeners:function(){
this.$scope.$on('$destroy',this.destroy.bind(this));
},
/**
* Use this function to define all scope objects.
* Give a way to instantaly view whats available
* publicly on the scope.
*/
defineScope:function(){
//OVERRIDE
},
/**
* Triggered when controller is about
* to be destroyed, clear all remaining values.
*/
destroy:function(event){
//OVERRIDE
}
})
BaseController.$inject = ['$scope'];
var PaginationAwareController = BaseController.extend({
_paginator: null,
init: function($scope, Paginator) {
this._paginator = Paginator.getInstance({
sortBy: this._defaultSortBy;
direction = this._defaultDirection;
pagingFunction = this.pagingFunction;
pagingSuccess = this.pagingSuccess;
pagingFailed = this.pagingFailed;
});
this._super($scope);
},
defineListeners: function() {
this._super();
},
defineScope: function() {
this.$scope.paginator = this._paginator;
},
destroy: function() {
//OVERRIDE
},
pagingFunction: function() {
//OVERRIDE
},
pagingSuccess: function(result, appendResult) {
//OVERRIDE
},
pagingFailed: function() {
//OVERRIDE
}
});
PaginationAwareController.$inject = ['$scope', 'Paginator'];
var TeamTransfersController = PaginationAwareController.extend({
teamService: null,
stateParams: null,
defaultSortBy: 'transferDate',
defaultDirection: -1,
init: function($scope, $stateParams, TeamService, Paginator) {
this.teamService = TeamService;
this.stateParams = $stateParams;
this._super($scope, $Paginator);
},
defineScope: function() {
this.$scope.isloaded = false;
this.$scope.data = [];
this.$scope.getNationalityFlag = _getNationalityFlag;
},
pagingFunction: function(paginationModel) {
return this.teamService.getTransfers(this.stateParams.id, paginationModel);
},
pagingSuccess: function(result, appendResult) {
if(result && result.length > 0) {
this.$scope.data = appendResult ? $scope.data.concat(result) : (result || []);
this.$scope.isloaded = true;
}
},
pagingFailed: function() {
this.$scope.data = [];
},
_getNationalityFlag: function(player) {
if(!player) {
throw "'player' parameter should be set when calling 'getNationalityFlag'";
}
return "http://beta.eliteprospects.com/images/flags/16/" + player.player.country.iso3166_3 + ".png";
}
});
TeamTransfersController.$inject = ['$scope', '$stateParams', 'TeamService', 'Paginator'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment