Last active
August 29, 2015 14:15
-
-
Save ptgamr/825868fc2d6378b686e4 to your computer and use it in GitHub Desktop.
AngularJS Controller Inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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