I'm neck-deep in refactoring/tidying a property editor, and curious as to how others are approaching large(ish) controllers?
I've been reading Todd Motto's posts around rethinking Angular controls, largely with regard to abstracting logic away from the controller and into factories, controllerAs syntax and essentially removing $scope from the controller.
While that all sounds great in theory, is it even possible in an Umbraco package, given the model data is encapsulated in $scope? Is there any value in passing the data object to a factory to perform some action, when that could be done in the controller - yes, it's logic in the controller, but it's not persisting anything as that's handled by Umbraco's services.
I'm going down the var vm = this; route, then assigning properties back to that object, rather than to $scope, as much because it's fewer keystrokes as anything (once the initial vm object exists - saves repeating $scope.model.value.xy throughout the controller):
var vm = this,
data = $scope.model.data;
vm.someVar = data.prop.childProp[2];
vm.anotherVar = data.anotherProp.prop.id;
It's far easier to read and comprehend (IMO).
ControllerAs then has benefits with nested controllers, but how often is that going to be happening in a property editor, which is typically a fairly simple interface to the model data?
I guess my question is how do we follow AngularJs best practice in property editors, given the reliance on $scope for delivering the model data? Is it even worth the time and effort?
Best practices for property editor controllers
I'm neck-deep in refactoring/tidying a property editor, and curious as to how others are approaching large(ish) controllers?
I've been reading Todd Motto's posts around rethinking Angular controls, largely with regard to abstracting logic away from the controller and into factories, controllerAs syntax and essentially removing $scope from the controller.
While that all sounds great in theory, is it even possible in an Umbraco package, given the model data is encapsulated in $scope? Is there any value in passing the data object to a factory to perform some action, when that could be done in the controller - yes, it's logic in the controller, but it's not persisting anything as that's handled by Umbraco's services.
I'm going down the
var vm = this;
route, then assigning properties back to that object, rather than to $scope, as much because it's fewer keystrokes as anything (once the initial vm object exists - saves repeating $scope.model.value.xy throughout the controller):can become:
It's far easier to read and comprehend (IMO).
ControllerAs then has benefits with nested controllers, but how often is that going to be happening in a property editor, which is typically a fairly simple interface to the model data?
I guess my question is how do we follow AngularJs best practice in property editors, given the reliance on $scope for delivering the model data? Is it even worth the time and effort?
is working on a reply...