Copied to clipboard

Flag this post as spam?

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


  • Isaiah Pellarp 19 posts 73 karma points
    Mar 03, 2021 @ 13:14
    Isaiah Pellarp
    0

    Custom Dashboard button click won't trigger my function

    I am currently trying to make a custom backoffice dashboard tool which all went well until I was trying to trigger the "generate" button on click.

    Package.Manifest:

    {
      "sections": [
        {
          "alias": "MapGenerator",
          "name": "Site Setup - MapGenerator"
        }
      ],
      "dashboards": [
        {
          "alias": "MapGeneratorDashboard",
          "view": "~/App_Plugins/MapGenerator/MapGenerator.html",
          "sections": [ "settings" ],
          "access": [
            { "grant": "admin" }
          ],
          "weight": 20
        }
      ],
      "javascript": [
        "~/App_Plugins/MapGenerator/mapgenerator.controller.js"
      ],
      "css": [
        "~/App_Plugins/MapGenerator/mapgenerator.css"
      ]
    }
    

    .html file:

    <div class="pictureoftheday" ng-controller="MapGeneratorController as vm">
        <umb-box>
            <umb-box-header title="{{vm.title}}"></umb-box-header>
            <umb-box-content>
    
                <div>
                    <span ng-bind="info"></span>
                    <button type="submit" class="btn btn-danger" ng-click="generating()">Generate</button>
                </div>
    
            </umb-box-content>
        </umb-box>
    </div>
    

    controller.js file (with some failed console.logs):

    angular.module("umbraco").controller("MapGeneratorController", function ($scope, userService, WorldGeneratorService, $http, $sce, postingLog) {
        var vm = this;
        vm.title = "Map Generator";
    
        userService.getCurrentUser().then(function (user) {
            vm.title = "Welcome " + user.name + ", to the map generator!";
        });
    
        $scope.generating = function () {
            console.log("henlo");
            $scope.info = "Getting all, please wait...";
            WorldGeneratorService.generateAll().success(function (data) {
                console.log(data);
                $scope.result = data;
                $scope.info = "Done!";
                console.log(data);
            });
        };
    
    });
    
    angular.module("umbraco.resources").factory("WorldGeneratorService", function ($http) {
        return {
            generateAll: function () {
                return $http.get("backoffice/Api/Admin/GenerateWorld");
            }
        };
    });
    

    controller in which I want the button click to lead me to eventually:

    namespace UmbracoBasic.Web.Controllers
    {
        public class AdminController : UmbracoAuthorizedController
        {
            // GET: Admin
            [HttpGet]
            public string GenerateWorld()
            {
               return "henlo";
            }
    
            [HttpPost]
            public string GenerateWorlds()
            {
                return "henlo";
            }
        }
    }
    

    So I get the custom dashboard in my admin view of umbraco, I see the button and everything else from the .html file but when I press the button absolutely nothing happens, I've tried some different ways in my .js file but none of them work (it won't even trigger the console.logs).

    As of now I'm at a loss and I'm having trouble finding any similar problems (I find several posts about custom dashboards but none about this specific button onclick issue im having).

  • Marc Goodson 2128 posts 14220 karma points MVP 8x c-trib
    Mar 03, 2021 @ 20:08
    Marc Goodson
    0

    Hi Isaiah

    I'm not saying 'this is it'... but usually if I'm using the

    var vm = this;

    approach then I'll define the functions I want to call from the view on the vm rather than the $scope eg:

             //have properties on the vm to track status
            vm.status = {
                isLoading: false,
                statusMessage: ''
            };
             //have properties on the vm to store the data returned
            vm.data = {
                result: [],
                hasResults: false
            };
            //declare the method on the vm
            vm.generating = generating;
    
        //define the function    
            function generating() {
               console.log("henlo");
    vm.status.isLoading = true;
    vm.status.statusMessage = 'Getting all, please wait...';
    
                    WorldGeneratorService.generateAll().success(function (data) {
                        console.log(data);
                        vm.status.isLoading = false;
                        vm.status.statusMessage = 'Done!';
                        vm.data.result = data;
                        vm.data.hasResults = true;
                        console.log(data);
                    });
             };
    

    and then in your view on your button

    ng-click="vm.generating()"
    

    regards

    Marc

  • Isaiah Pellarp 19 posts 73 karma points
    Mar 04, 2021 @ 10:20
    Isaiah Pellarp
    0

    Hi Marc, thanks for the reply! Unfortunately this still does nothing when I press my button..im not really sure why vm.title gets printed in my tool but not the button function..

  • Marc Goodson 2128 posts 14220 karma points MVP 8x c-trib
    Mar 04, 2021 @ 17:58
    Marc Goodson
    0

    Hi Isaiah

    are there any errors in the console?

    and if you just add an alert('hello'); to the start of the controller - are you getting that appearing...

    One of the things that completely bamboozled me when I started with Umbraco and AngularJS, is the monumental level of 'caching' that occurs, so for ages I was stuck, pressing Ctrl-F5 and nothing working, whatever I tried, but then I found, that there is an option in Chrome to 'not cache' when developer tools was open.

    And with Developer Tools open, if you right-click the 'refresh' icon in the browser there is a further menu item called 'Empty cache and hard reload'... and that is what I've found always forces a fresh load of my angularJS implementation...

    so it may be worth just trying that alert message and a complete refresh to rule out that caching is the culprit!

    regards

    marc

  • Isaiah Pellarp 19 posts 73 karma points
    Mar 04, 2021 @ 21:38
    Isaiah Pellarp
    0

    Hey Marc,

    Ye I usually rightclick hard-refresh and clear cache on each refresh (habit by now lol). At the start of the controller I put the vm.title string which I can see work..I could try with alert also, but what would that tell me? Why would title work and not anything else?

  • Marc Goodson 2128 posts 14220 karma points MVP 8x c-trib
    Mar 05, 2021 @ 20:54
    Marc Goodson
    0

    Hi Isaiah

    good point, so not caching then! and yes right click clear cache and full reload becomes very instinctive...

    ... I've spotted something else, but it depends a little on which console.logs are not being written out...

    Your controller that you are making a GET request to inherits from UmbracoAuthorizedController, but this is an MVC controller, I think you need to inherit from UmbracoAuthorizedApiController

    https://github.com/umbraco/Umbraco-CMS/blob/34e80d86e8c0b754f6b7a02e307f53cb32806bbe/src/Umbraco.Web/WebApi/UmbracoAuthorizedApiController.cs

    The UmbracoAuthorizedController won't be routed via backoffice/Api/Admin/GenerateWorld

    Only if you inherit from UmbracoAuthorizedApiController....

    but you shoudl see the first console.log when the generating() method is called before the api request, and you should see a 404 when the Get request is made... so maybe this isn't the problem now (but will be the problem when you get this far... :-))

    regards

    Marc

  • Isaiah Pellarp 19 posts 73 karma points
    Mar 15, 2021 @ 17:27
    Isaiah Pellarp
    0

    The issue turned out to be that umbraco cached everything and that somehow prevented any action from the button click to be ignored..felt slightly dumb when I realized this I must say :D

    But thank you for all the help along they of me discovering the cause of the issue, much appreciated!

    sincerely, Isaiah

  • Marc Goodson 2128 posts 14220 karma points MVP 8x c-trib
    Mar 15, 2021 @ 17:32
    Marc Goodson
    1

    Glad it's all working! - pain when it's things like this, but I guess that is our developer lives through and through! - onto the next problem now!

  • Brendan Rice 537 posts 1098 karma points
    Mar 15, 2021 @ 19:34
    Brendan Rice
    1

    enter image description here

    I keep dev tools open when working in the backoffice with this enabled. It gets around the caching issue.

  • Isaiah Pellarp 19 posts 73 karma points
    Mar 15, 2021 @ 21:00
    Isaiah Pellarp
    0

    Oh ye, should start doing this more. Does this have the same effect as setting debug=true in web.config?

  • Brendan Rice 537 posts 1098 karma points
    Mar 16, 2021 @ 10:37
    Brendan Rice
    0

    No debug=true is something different. It compiles the code/dlls in debug mode and generates pdbs allowing you to step through the code.

Please Sign in or register to post replies

Write your reply to:

Draft