Skip to content

Instantly share code, notes, and snippets.

@benbabics
Last active August 29, 2015 14:07
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 benbabics/b29fad88f2866770f382 to your computer and use it in GitHub Desktop.
Save benbabics/b29fad88f2866770f382 to your computer and use it in GitHub Desktop.
AngularJS BaseController
(function() {
var app, ApplicationController;
ApplicationController = window.BaseController.extend({
inject: ['$rootScope', 'MyService'],
initialize: function($scope, $rootScope, MyService) {
// (optional) intitialize stuff
},
defineScope: function($scope, $rootScope, MyService) {
// define scope
},
defineListeners: function($scope, $rootScope, MyService) {
// (optional) assign listeners
},
destroy: function(evt) {
// (optional) remove listeners, etc
},
handleItemSelection: function(item) {
// (magic method on $scope) do something
}
});
app = angular.module('App', []);
app.controller('ApplicationController', ApplicationController);
})();
// Inspired by: http://trochette.github.io/Angular-Design-Patterns-Best-Practices/#/inheritance_pattern
(function() {
function BaseController() {
var args, fn, index, key, i, len, dependencies, members, collaborators;
args = 1 <= arguments.length ? [].slice.call(arguments, 0) : [];
collaborators = [];
dependencies = this.constructor.$inject;
for (index = i = 0, len = dependencies.length; i < len; index = ++i) {
key = dependencies[index];
this[key] = collaborators[index] = args[index];
}
members = this.constructor.prototype;
for (key in members) {
fn = members[key];
if (typeof fn !== 'function') continue;
if ((key === 'constructor' || key === 'initialize') || key[0] === '_') continue;
fn = (function(context, fn) {
return function() {
return fn.apply(context, arguments);
};
})(this, fn);
this[key] = fn;
if (key.lastIndexOf('handle', 0) >= 0) {
this.$scope[key] = fn;
}
}
this.defineListeners.apply(this, collaborators);
this.initialize.apply(this, collaborators);
this.defineScope.apply(this, collaborators);
(function(context) {
context.$scope.$on('$destroy', function(evt) {
context.destroy(evt, context.$scope);
});
})(this);
}
BaseController.extend = function(protoProps) {
var parent = this,
collaborators = protoProps.inject || [];
delete protoProps.inject;
function child() {
child.__super__.constructor.apply(this, arguments);
}
for (var key in parent) {
if ({}.hasOwnProperty.call(parent, key)) {
child[key] = parent[key];
}
}
function Surrogate() {
this.constructor = child;
}
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate();
child.__super__ = parent.prototype;
for (var key in protoProps) {
if ({}.hasOwnProperty.call(protoProps, key)) {
child.prototype[key] = protoProps[key];
}
}
if (collaborators.indexOf('$scope') < 0) {
collaborators.unshift('$scope');
}
child.$inject = collaborators;
return child;
};
// Abstract Methods
BaseController.prototype.initialize = function() {};
BaseController.prototype.defineScope = function() {};
BaseController.prototype.defineListeners = function() {};
BaseController.prototype.destroy = function() {};
// Expose the BaseController
window.BaseController = BaseController;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment