-2

I understand the basic Javascripts callbacks, but I do not know what $rootScope.$emit 'dispatchQuery' is doing.

(function() {
  angular.module('sp').factory('Object', [
    '$rootScope', function($rootScope) {
      var Object;
      return Object = {
        load: function(callback) {
          return $http(method: "get", url: "http://myhost/bananas", options = {}).success(function(response) {
            $rootScope.myItems = response.items;
            return callback && callback();
          });
        }
      };
    }
  ]);
}).call(this);

it is called from main.js

sp.run [
  '$rootScope','Object'
, function($rootScope, Object) {
  Object.load = function(){ $rootScope.$emit 'Query'}
}]
Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57
  • 2
    [`$rootScope.$emit` and `$rootScope.$broadcast`](https://stackoverflow.com/questions/26752030/rootscope-broadcast-vs-scope-emit) let event listeners (`$rootScope.$on(...)`) to catch their events. After you broadcasted (across your entire app) your event, you can listed for it and **call some function** to be executed `$rootScope.$on('dispatchQuery', someFunction)` – Aleksey Solovey Apr 04 '18 at 15:34
  • You can add your own bounty on it, but considering you posted it 2 minutes ago, you may want to wait. You might also want to add more details as to what research you've done. – Heretic Monkey Apr 04 '18 at 15:34
  • @MikeMcCaughan I did not do that, because too long question are not read. I did my research based on the available time. If I had more time I would just start learning the fundamentals of angular. I would avoid downvoting so that I can add a bounty – Fabrizio Bertoglio Apr 04 '18 at 15:36
  • 1
    I'm guessing that this code isn't *exactly* as it exists in the real app.... wouldn't it be `function(callback)` rather than `function(cb)`? – Claies Apr 04 '18 at 15:36
  • @Claies yes, you are right. I made a mistake, I just updated the question. I am available to provide any additional information. – Fabrizio Bertoglio Apr 04 '18 at 15:38
  • @FabrizioBertoglio Regarding research: please see [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/1288408) Showing your research is important. – Modus Tollens Apr 04 '18 at 15:40
  • 1
    "I am available to provide any additional information"... except the research you did? Your need for a quick answer does not exempt you from having to follow the rules. A simple search on $rootScope.$emit would get you the answer faster. – Heretic Monkey Apr 04 '18 at 15:42
  • @AlekseySolovey Ok.. That is really helpful. Thanks, as my understanding now it is like `$('.my-div-class').on('dispatchQuery', someFunction)`. Still I need to read more and understand what exactly `$rootScope` and `$scope` are. I see that they include several `variables`/`infos` about the `directives` and `factories`, but I do not have a clear understanding of it. – Fabrizio Bertoglio Apr 04 '18 at 15:42
  • 1
    `$(..).on(..)` is a jQuery event handler, not an AngularJS event handler. The two event systems do not work together (by default). – JC Ford Apr 04 '18 at 15:44
  • 3
    If you are unsure of "the basics of angular", (i.e. you aren't sure what `$rootScope` or `$scope` are), This is probably not the best code to start with trying to dissect; it's not really a very well written piece of code, and it doesn't do anything particularly useful that couldn't be handled in some other manner, beside the fact that `$emit` is only half of the picture. – Claies Apr 04 '18 at 15:45
  • @MikeMcCaughan You are right. I read https://stackoverflow.com/questions/14502006/working-with-scope-emit-and-scope-on#14502755 and https://stackoverflow.com/questions/49630669/http-saving-result-with-callback-function?noredirect=1#comment86269634_49630669 and the angularjs documentation. Do you believe this question is not worth a **bounty**? It could avoid me some unpleasant situation – Fabrizio Bertoglio Apr 04 '18 at 15:47
  • 2
    Bounties are used to draw attention to questions. Basically paid advertising for a question. Or possibly to award a particularly good post. You're already getting answers, so why would you need a bounty? – Heretic Monkey Apr 04 '18 at 15:49
  • @MikeMcCaughan if I do not solve this issue within two days, I'll still be able to clarify my issues and rephrase the question. The main issue is my ignorance on the subject. – Fabrizio Bertoglio Apr 04 '18 at 15:52
  • 4
    @MikeMcCaughan some people think that if they post a bounty it's like paying programmers so they don't have to learn the techniques themselves. In this case, it's clear that the OP has never used angular before but is trying to fix ***something*** **somewhere** in the app, and started here, though this code isn't really even worth trying to understand. – Claies Apr 04 '18 at 15:53
  • 1
    @Fabrizio Hey, you're free to use your rep as you wish. But it sounds like your time might be better spent reading tutorials and searching the internet than worrying about bounties. – Heretic Monkey Apr 04 '18 at 15:54
  • 1
    The problem is, your lack of understanding of very basic angularjs ideas is going to make it extremely difficult for you to be able to use any of the help given for this particular question in the next two days. The learning curve for angularjs is pretty steep, and I would recommend following a tutorial to build something simple like a todo app before trying to understand `$emit()` and some of the more complex ideas angular uses. – Tyler Apr 04 '18 at 15:55
  • @Claies I am a Rails developer. I added an API endpoint to our backend and I need to retrieve this information with the `$http()` in our `angularjs` app, but I had many issues by setting my `callback` function in `angularjs` (bugs etc..) and my last resort is review how the previous developer set up the `API` and emulate his functionalities (The code above). Now I have a better understanding of `$emit` and I am more confident. THANKS A LOT to Everyone for the help – Fabrizio Bertoglio Apr 04 '18 at 15:56
  • @Tyler yes, you are right. I will start building an Angularjs app tonight. I thought that I could just learn it by doing it, but it is not the right approach. – Fabrizio Bertoglio Apr 04 '18 at 15:58
  • 3
    right, so instead of asking how a random piece of code someone else wrote works, it's generally better to post the code **you wrote** that doesn't work, and ask why you aren't getting the result you expect. This code uses some pretty out of date concepts that aren't really even recommended in new AngularJs apps. – Claies Apr 04 '18 at 15:58
  • I am starting the angularjs training right now – Fabrizio Bertoglio Apr 04 '18 at 16:04

1 Answers1

1

$rootScope.$emit() dispatches event messages upward through the scope chain. Since $rootScope is at the top of the scope chain, only listeners on $rootScope will receive the event. Somewhere in your code, there should be a $rootScope.$on('Query', function(){...})

However, the syntax of your code seems wrong. The code below will generate a syntax error.

$rootScope.$emit 'Query'

Also, your factory creates a .load() method which is then replaced with a different method in your .run() block. Nowhere in your code sample is either method actually called.

Finally...you should avoid using the keyword Object. Consider giving your service factory a more meaningful name.

JC Ford
  • 6,946
  • 3
  • 25
  • 34