Establish the directive upon receiving the broadcast

Is there a way to trigger a directive when a specific event occurs on the backend and sets a value to false?...

This is where the event is being captured


    .factory('AuthInterceptor', function($q, $injector, $rootScope) {
        return {
            response: function(response) {
                if (response.data.lineStatus) {
                    $rootScope.$broadcast('line:lineStatus', response.data.lineStatus);
                }
                return response;
            }
        });

Then, I handle the broadcast in this section


    .factory('BetSlipFactory', function($rootScope) {
        var processingLineMoves = false;

        $rootScope.$on('line:lineStatus', function(ev, status) {
            status.selections = _.map(status.selections, function(selection) {
                selection.display = false;
                return selection;
            });
            if (!status.linesOK) {
                if (!processingLineMoves) {
                    processingLineMoves = true;
                    $rootScope.linesMovedModal = $popover(angular.element('#linesMovedModal'), {
                        template: 'views/linesMovedModal.html',
                        html: true,
                        autoClose: false,
                        placement: 'left',
                        trigger: 'manual',
                        animation: 'fx-fade-down',
                        scope: _.assign($rootScope.$new(), status, {
                            okPicks: function(selection, selections, index) {
                                if (selections[index + 1] || selections.length > 1) {
                                    $rootScope.betLoader = true;
                                    selections.splice(index, 1);
                                    $rootScope.betLoader = false;
                                } else {
                                    $rootScope.linesMovedModal.hide();
                                    processingLineMoves = false;
                                    selections.splice(index, 1);
                                }
                            },
                            removePick: function(selection, selections, index) {
                                console.log('selections', selections);
                                betSlipSelectionRequest('/betSlip/removeSelection', {
                                    game: selection.game,
                                    pair: selection.pair,
                                    line: selection.line
                                }).then(function() {
                                    selections.splice(index, 1);
                                    $rootScope.$broadcast('lines:removeAllLines');
                                    if (selections[index + 1] || selections.length > 1) {
                                        $rootScope.betLoader = true;
                                        $rootScope.betLoader = false;
                                    } else {
                                        $rootScope.linesMovedModal.hide();
                                        processingLineMoves = false;
                                    }
                                }, function(err) {
                                    console.log(err);
                                });
                            }
                        })
                    });
                    $timeout(function() {
                        $rootScope.linesMovedModal.show();
                    }, 800);
                }
            }
        });

    });

Essentially, the $rootScope.$on part needs to be integrated into a directive. The purpose is to activate a popover that shows some information, with the starting point being the $rootScope variable: $rootScope.linesMovedModal

The reason for transitioning this to a directive is due to the usage of DOM manipulation using

$popover(angular.element('#linesMovedModal')

In case you are interested, here is the link to the popover component I am utilizing: Popover Component

Answer №1

.directive('customDirective', function($rootScope, $popover, $timeout, BetSlipFactory) {
  return {
    restrict: 'E',
    link: function(scope, element, attrs) {
      var processingMoves = false;
      scope.$on('line:lineStatus', function(ev, status) {
        status.selections = _.map(status.selections, function(selection) {
          selection.display = false;
          return selection;
        });
        if (!status.linesOK) {
          if (!processingMoves) {
            processingMoves = true;
            scope.customModal = $popover(element, {
              template: 'views/customModal.html',
              html: true,
              autoClose: false,
              placement: 'bottom',
              trigger: 'manual',
              animation: 'fx-fade-down',
              scope: _.assign(scope.$new(), status, {
                okayPicks: function(selection, selections, index) {
                  if (selections[index + 1] || selections.length > 1) {
                    scope.betLoader = true;
                    selections.splice(index, 1);
                    $timeout(function() {scope.betLoader = false;}, 100);
                  } else {
                    scope.customModal.hide();
                    processingMoves = false;
                    selections.splice(index, 1);
                  }
                },
                deletePick: function(selection, selections, index) {
                  CreateNewFunctionOnService(selection).then(function() {
                    $rootScope.$broadcast('lines:removeAllLines');
                    if (selections[index + 1] || selections.length > 1) {
                      selections.splice(index, 1);
                      $rootScope.betLoader = true;
                      $timeout(function() {scope.betLoader = false;}, 100);
                    }else {
                      $rootScope.customModal.hide();
                      processingMoves = false;
                      selections.splice(index, 1);
                    }
                  }, function(err) {
                    console.log(err);
                  });
                }
              })
            });
            $timeout(function() {
              scope.customModal.show();
            }, 200);
          }
        }
      });
    }
  };
});

Feel free to give it a try and provide feedback

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I effectively exclude API keys from commits in Express by implementing a .gitignore file?

Currently, my API keys are stored in the routes/index.js file of my express app. I'm thinking that I should transfer these keys to an object in a new file located in the parent directory of the app (keys.js), and then include this file in my routes/in ...

Using JSON parsing to dynamically create classes with preloaded background images

Today, I successfully deployed my browser game using MVC4 to my website for the first time. I am currently navigating through the differences between running the site off of localhost and running it from the actual website. My process involves loading all ...

Creating scalable controllers in ExpressJS

Recently diving into Node, I am venturing into the realm of creating an MVC app with ExpressJS. To mimic the structure of a well-known MVC example found on GitHub, I have organized my controllers into two main folders: main and system. My goal is to establ ...

Tips for creating a dashed border line that animates in a clockwise direction when hovering

I currently have a dashed border around my div element: .dash_border{ border: dashed; stroke: 2px; margin:20px; } My goal is to make the dashed lines move clockwise when my pointer hovers over the div element and stop moving ...

The test does not pass when attempting to use a shorthand operator to ascertain the truthfulness of

I've encountered an interesting issue with my unit test. It seems to work perfectly fine when I directly return true or false, but fails when I try to use a shorthand method to determine the result. Let's say I have a function called isMatched w ...

Bump the version number and request a message for the commit

Recently diving into the world of Grunt, I've been experimenting with merging grunt-bump and grunt-prompt. My intention is to have users prompted for a commit message that will then be added to the commit. I looked to this article for guidance in str ...

Swapping out a class or method throughout an entire TypeScript project

Currently, I am working on a software project built with TypeScript. This project relies on several third-party libraries that are imported through the package.json file. One such library includes a utility class, utilized by other classes within the same ...

The error message "Potree-Core: THREE is undefined" indicates that the

I'm currently developing a web-based application that can be used offline by other users on their local machines. Within my NPM project, I am utilizing Potree-Core, which relies on THREE.js. However, the source code does not include an import for THR ...

Surfing the web with Internet Explorer means music downloads rather than streaming

My music website functions properly, however I am experiencing issues when accessing it through Internet Explorer. The internet download manager is downloading music from the site without any problems in Chrome and Firefox. Is there a way to prevent or b ...

Having trouble integrating a custom plugin with CKEditor?

I am currently using version 4.7 of CKEditor, which is the latest version available. I am facing an issue where I cannot see a new custom plugin that I have added to the toolbar. I have checked my basic example of abbreviation (abbr) plugin but couldn&apos ...

Unforeseen alterations in value occur in JavaScript when converting to JSON format

Having trouble generating a gantt chart from a JSON string, specifically with parsing the JSON string into a JSON object. I have a variable myString containing a JSON string that looks like this: {"c": [{"v": "496"}, {"v": "Task name 1"}, {"v": "9, "}, { ...

What is the hierarchy for displaying elements depending on the props?

I have developed a search input component that includes an icon which I want to reposition (either on the left or right side) depending on different scenarios. This input is part of a bootstrap input-group, so modifying the order of elements within my di ...

Including a unicode escape sequence in a variable string value

I'm struggling to find the right way to include a unicode escape in a dynamic string value to display emojis in React. My database stores the hexcode for the emoji (1f44d) I have set up a styled-component with the necessary css for rendering an emoj ...

Having trouble getting Material-UI classes to work with external SVG icons?

I recently crafted a Material-UI persistent drawer that contains a list item component designed to alter the icon color upon user interaction. However, my styling adjustments only seem to apply to Material-UI icons and not external SVG ones. If you'r ...

Puzzling array challenge. Lack of clarity in explanation

I am currently working on a series of JavaScript tests available at js-assessment One of the tasks states: it("you should be able to find all occurrences of an item in an array", function() { var result = answers.findAllOccurrences('abcdefab ...

Revamp the layout of the lower HTML video menu

Here is my code: <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> video { width: 100%; height: auto; } </style> </head> <body> <video width="400" controls> ...

Issue: The variable '$viewMap[...]' is either null or undefined

Unfortunately, my knowledge of jQuery/Javascript is quite limited. I am encountering a Javascript error when trying to change the "how did you hear about us" dropdown on a form. The error message states: Error: '$viewMap[...]' is null or not an ...

Changing the information of objects stored in arrays using React Three Fiber

My challenge is with an array of roundedBox geometry shapes called myShape. I am trying to figure out if it's possible to change the position of one of the shapes within the array without creating a new shape altogether. Ideally, I would like to updat ...

Struggling to fill in fields using express and Mongodb

I am facing an issue with populating the fields of two models, Post and User. const PostSchema = new Schema({ text: { type: String }, author: { type: mongoose.Schema.Types.ObjectId, ref: 'user' } }) cons ...

Insert the object into a designated location within a multi-dimensional array

I've integrated a tree view into my Angular project and I'm looking to add an object to a specific position within an array. array const TREE_DATA: TreeNode[] = [{"name":"Demo","id":"demo_1","children ...