So here's what I could come up with for now. Please feel free to comment if
this is or is not a good approach.
I started within one IIFE to decalre my task routine logic:
var utilModule = angular.module('utilModule');
function TaskMonitorController(...){
...
this.setTitle = function(title){
$scope.taskTitle = title;
};
this.submit = function(url){
... pre-logic ...
$http.get(url).then(...); // reference internal callbacks to change scope
properties for success/fail.
};
}
I decided not to register the above into an angular controller this time,
but instead to save the constructor reference so I could use it later in my
other modules. I didn't want to put it in the global scope, and wanted to
keep it within angular so I made a constant.
utilModule.constant('taskMonitorController', TaskMonitorController);
Now let's say one of the places where I want to reuse this controller logic
is: moduleA and controllerA, and that these are decalred in a separate IIFE
var moduleA = angular.module('moduleA', ['ngRoute', 'utilModule']);
To get a reference to the taskMonitorController, I don't see a very clean
way to get a constant resolved when outside of a angular invocation chain
so:
var taskMonitorController =
angular.injector(['moduleA']).get('taskMonitorController');
function ControllerA(...same params matching TaskMonitorController...){
taskMonitorController.call(this, params from above...); // i.e $scope,
$http, etc.
this.setTitle('my title');
this.submit('/my-service-path');
}
ControllerA.prototype = Object.create(taskMonitorController.prototype);
ControllerA.prototype.constructor = ControllerA;
moduleA.controller('controllerA', [params matching constructor...,
ControllerA]);
How do people feel about passing the base portion of the controller
constructor TaskMonitorController to another module, extending, and
registering it this way? Is there any room for optimization or improvement?
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.