Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1470 posts 3427 karma points c-trib
    Nov 15, 2018 @ 10:45
    Simon Dingley
    0

    Intercepting a Core Directive in Angular

    I have a requirement to provide read-only access to member data and I have almost achieved this apart from one key property, the email address!

    I have achieved most of what I need so far by using interceptors to replace the view urls with my own which reference my own controllers which extend the core ones rather than duplicating them. A simplified example as follows:

    angular.module('umbraco.services').config([
        '$httpProvider',
        function($httpProvider) {
    
            $httpProvider.interceptors.push([
                '$q', '$injector', 'notificationsService', function($q, $injector, notificationsService) {
                    return {
                        'request': function(request) {
    
                            // Redirect any requests to built in property editor and instead redirect to our own
                            if (request.url.indexOf("views/propertyeditors/textbox/textbox.html") === 0) {
                                request.url = '/App_Plugins/MyPlugin/PropertyEditors/securetextbox.html';
                            }
    
                            return request || $q.when(request);
                        }
                    };
                }
            ]);
    
        }
    ]);
    

    Any my custom controller as follows:

    angular.module('umbraco').controller('MyPlugin.PropertyEditors.SecureTextboxController', function ($scope, $controller, memberDataAccessResource, $element) {
    
        memberDataAccessResource.hasAccess($scope.model.alias).then(function (result) {
            $scope.model.hasAccess = result;
        });
    
        angular.extend(this, $controller('Umbraco.PropertyEditors.textboxController',
            {
                $scope: $scope,
                $element: $element
            }));
    });
    

    This is an achievement in itself for me as I am no Angular expert! However, I am now at a roadblock with the email property as it using directive and unlike the other views which allow me to reference my own controller, I don't seem to have that luxury with a directive.

    Can anyone offer some advice or potential solutions on how to tackle this, please?

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Nov 16, 2018 @ 10:40
    Dave Woestenborghs
    100

    Hi Simon,

    I thinks this can be done easier without any angular hacking (I know i am partially responsible for this because of my skrift article :-D )

    Have a look at the editor model events : https://our.umbraco.com/documentation/Reference/Events/EditorModel-Events/

    They can be used for members as well.

    Have a look here on how I make properties ready for content items : https://github.com/dawoe/umbraco-journey-into-the-unknown/blob/master/App_Code/EditorModelEvents.cs#L87

    In this example a field is only editable for admins. All other users will see it as readonly

    The same can be done for members.

    Dave

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Nov 16, 2018 @ 16:36
    Simon Dingley
    0

    Wow, thanks Dave - How did I not know about this before now!? Looks exactly the sort of thing I need so will have a play with it and mark your answer as the solution if successful.

    I owe you a beer or two for sure by now :)

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Nov 16, 2018 @ 16:52
    Simon Dingley
    0

    This works perfectly thanks. I'm so glad you have drawn my attention to this as I can see it being immensely useful in so many other projects!

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Nov 16, 2018 @ 17:13
    Simon Dingley
    0

    I have one additional question relating to this implementation of making the fields read-only. Is it also possible to disable the save button in this scenario? In my case I have a few custom property editors on the member and so making the fields read-only means the value displayed to users in some cases makes no sense as it just raw data. As such I am converting the value before rendering the read-only version however if the user clicks save in the read-only view they end up saving the converted data and losing the original value(s).

    Edit: I know I could cancel the save event but I would prefer not to even present the option in the first place.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Nov 17, 2018 @ 08:48
    Dave Woestenborghs
    0

    Will try see if it is possible. Feel free to send me a reminder if I forget.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft