generator-angular-fullstack icon indicating copy to clipboard operation
generator-angular-fullstack copied to clipboard

Usage of IIFE pattern

Open sedina opened this issue 10 years ago • 6 comments

The IIFE pattern would be very useful to have within the generator. Is this pattern left out on purpose, for what reason?

sedina avatar Dec 11 '15 08:12 sedina

We do use them, for ex in admin.controller.js. IIFEs are useful for preventing references from polluting the global scope of the browser. If we're not concerned with something polluting the global scope, there's no point in using an IIFE. Using IIFEs for makeshift modules isn't a good practice nowadays also.

Awk34 avatar Dec 13 '15 07:12 Awk34

Yes I do agree that IIFE is very useful as an extra prevention step, that's why I consider it as very important pattern. You are referring to the admin.controller.js where the pattern is being used, but my question is more referring auto-usage of this pattern when for instance generating a service/controller I think it would be good have IIFE generated within the code skeleton. What's your opinon regarding this case?

sedina avatar Dec 13 '15 11:12 sedina

Could you give a code example of what you're referring to?

Awk34 avatar Dec 13 '15 19:12 Awk34

if I run: yo angular-fullstack:service myService

then I would like to get something like:

(function() {
  'use strict';

  angular.module('myApp')
  .service('myService', function () {
    // AngularJS will instantiate a singleton by calling "new" on this function
  });
})();

sedina avatar Dec 15 '15 07:12 sedina

Agree with @sedina because several parts already covered with IIFE (see out-of-box main controller implementation).

'use strict';
(function() {

function MainController($scope) {
}

angular.module('app')
  .controller('MainController', MainController);
})();

Would be good to have consistency in every module.

donvadicastro avatar Dec 15 '15 08:12 donvadicastro

Yeah, @sedina in your example I don't see a point in using an IIFE there, since you're not instantiating any variables that would be attached to the global scope / window there. Also note, the client-side subgenerators (such as the service generator) are not part of this project, but imported from generator-ng-component.

@donvadicastro your example could warrant an IIFE. It's not too likely that a variable named MainController would collide, but better safe than sorry I guess? There are a few more spots that could use it.

Awk34 avatar Dec 15 '15 15:12 Awk34