javascript - Angular controller inheritance / overriding "superclass" methods -
when extend controller in angular, there way call function on "superclass" controller "subclass" function overrides it?
for clarity - in java i'd do:
class foo { void dostuff(){ //do stuff } } class foobar extends foo { void dostuff(){ super.dostuff(); //do more stuff } }
and i'd equivalent in angular -
myapp.controller('fooctrl', function($scope){ $scope.dostuff = function(){ //do stuff } }).controller('foobarctrl', function($scope){ angular.extend(this, $controller('fooctrl', {$scope: $scope})); $scope.dostuff = function(){ // ??? <- insert answer here //do more stuff } }
i wouldn't recommend pattern, answer question, here way it:
myapp.controller('fooctrl', function($scope){ $scope.dostuff = function(){ //do stuff } }).controller('foobarctrl', function($scope){ angular.extend(this, $controller('fooctrl', {$scope: $scope})); //extend scope var super = angular.extend({}, $scope); $scope.dostuff = function(){ // ??? <- insert answer here //do more stuff //call "superclass" methods if(super.dostuff){ super.dostuff(); } } }
spitballing, suppose of write helper service allowed override properties references superclass implementations make cleaner. perhaps overriding "this". like:
$scope.dostuff = $override($scope.dostuff, function() { this(); //calls original dostuff function }); .factory('$override', function(){ return function(method, func){ return function(){ return func.apply(method, arguments); }; }; });
Comments
Post a Comment