What is the difference between init () and window.init ()?

I read the following recipe that shows how to use the AngularJS interface with the Google Cloud Endpoints backend:

https://cloud.google.com/resources/articles/angularjs-cloud-endpoints-recipe-for-building-modern-web-applications

What I don't understand is an application for initializing AngularJS and Cloud Endpoints. The relevant section is as follows:

Appendix: AngularJS + Cloud End # 1 Endpoint Configuration Tips: Be careful with the initialization sequence

The guestbook application loads three different JS libraries in the following sequence:

  • Angularjs
  • Guestbook Application
  • Google API Client Containing Endpoints Features

To follow this sequence, index.html contains the following <script> in the <head> to load each of the JS libraries:

 <script src="js/angular.min.js"></script> <script src="js/guestbook.js"></script> <script src="https://apis.google.com/js/client.js?onload=init"></script> 

After loading, the third library (Google API Client) calls the initialization function specified by its onload parameter. In this case, the init () function is expected and called. Tip # 2: enter AngularJS World as quickly as possible

In the initialization sequence, we use two functions:

 init() function window.init() function 

This init () function is defined in the .js guestbook as follows:

 function init() { window.init(); } 

As you can see the above code, the function simply calls the window.init () function (i.e. the init () function defined in the global window object) and does nothing. Window.init () is defined in an AngularJS controller as follows:

 $window.init= function() { $scope.$apply($scope.load_guestbook_lib); }; 

In AngularJS, the global window object is referenced by the "$ window" notation, which is a wrapper for it. It is best practice in AngularJS not to access the window object directly in order to improve testability.

The reason you don't want to initialize the first init () method is because you can put as much code as possible in the AngularJS world, such as controllers, services, and directives. As a result, you can use all the power of AngularJS and all your unit tests, integration tests, etc.

It seems that the global init() function is defined in an external javascript file. This init() function simply calls window.init() (and should be called by the Google client library after loading it). But is window.init () nothing but the global init() function? So, won't we get a loop here until window.init() (and therefore init() ) is overridden?

+6
source share
2 answers

This example tries to take the google init api event and move it to the AngularJS scope.

When

 <script src="https://apis.google.com/js/client.js?onload=init"></script> 

it calls the init function, defined globally, which, in turn, calls the init method defined on the window object. Since this function has access to angular areas, it works well with angular

This simplifies the transfer of endpoint calls in the cloud to promises.

Here's how to make it a lot easier http://anandsekar.imtqy.com/initialize-google-appengine-and-angularjs/

+1
source

guestbook.js defines an init function that appears from your description to be global. This init function is passed to google client.js. This global init function simply calls the (other) global function in window.init. First, the loaded angular module should have setup window.init through the angular provided window $. There is no loop, client.js calls guestbook.js init, which calls the angular $ window.init method, which is the same as window.init.

0
source

Source: https://habr.com/ru/post/952848/


All Articles