Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nathan Woulfe 447 posts 1664 karma points MVP 5x hq c-trib
    Feb 16, 2016 @ 08:32
    Nathan Woulfe
    0

    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):

    $scope.someVar = $scope.model.value.data.prop.childProp[2];
    $scope.anotherVar = $scope.mode.value.data.anotherProp.prop.id;
    

    can become:

    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?

Please Sign in or register to post replies

Write your reply to:

Draft