/* Feature areas */
'app.welcome',
- 'app.cars'
+ 'app.cars',
+ 'app.users'
]);
}());
--- /dev/null
+(function () {
+ 'use strict';
+
+ angular
+ .module('app.users')
+ .controller('UsersChildController', UsersChildController);
+
+ /**
+ * @ngdoc controller
+ * @name app.users.controller:UsersChildController
+ *
+ * @requires $rootScope
+ *
+ * <p>
+ * <br>
+ * {@link https://docs.angularjs.org/api/ng/service/$rootScope $rootScope}
+ * </p>
+ *
+ * @description
+ * Users controller.
+ */
+ /* @ngInject */
+ function UsersChildController($rootScope, $scope, USERS) {
+ var vm = this;
+ var emitFact = {
+ title: 'Snake and Scarlett',
+ fact: 'it is canon'
+ };
+
+ vm.getEmit = function () {
+ $scope.$emit(USERS.SCOPE.EMIT_FACT, emitFact);
+ };
+
+ // NEVER USE $rootScope.$on IN CONTROLLER BECAUSE IT IS NOT DESTROYED EVEN IF CONTROLLER WAS DESTROYED!!!
+ // YOU WILL END UP HAVING AS MANY EVENT LISTENERS AS TIMES THIS CONTROLLER IS CREATED!!!!
+ // $rootScope.$on(USERS.ROOTSCOPE.BROADCAST, usersChildOnRootBroadcast);
+
+ // LISTENING FOR EVENTS IN $scope IS THE RIGHT THING BECAUSE THESE EVENT LISTENERS ARE DESTROYED
+ // AT THE SAME TIME AS THIS CONTROLLER :)
+ $scope.$on(USERS.ROOTSCOPE.BROADCAST, usersChildOnScopeBroadcast);
+
+ // function usersChildOnRootBroadcast(events, broadcastUser) {
+ // vm.broadcastUser = broadcastUser;
+ // console.log('usersChildOnRootBroadcast, events.name: ' + events.name);
+ // }
+
+ function usersChildOnScopeBroadcast(events, broadcastUser) {
+ vm.broadcastUser = broadcastUser;
+ console.log('usersChildOnScopeBroadcast, events.name: ' + events.name);
+ }
+
+ }
+})();
--- /dev/null
+(function () {
+ 'use strict';
+
+ angular
+ .module('app.users')
+ .constant('USERS', {
+ ROOTSCOPE: {
+ BROADCAST: 'USERS_ROOTSCOPE_BROADCAST'
+ },
+ SCOPE: {
+ EMIT_FACT: 'USERS_SCOPE_EMIT_FACT'
+ }
+ });
+
+})();
--- /dev/null
+(function () {
+ 'use strict';
+
+ angular
+ .module('app.users')
+ .controller('UsersController', UsersController);
+
+ /**
+ * @ngdoc controller
+ * @name app.users.controller:UsersController
+ *
+ * @requires $rootScope
+ *
+ * <p>
+ * <br>
+ * {@link https://docs.angularjs.org/api/ng/service/$rootScope $rootScope}
+ * </p>
+ *
+ * @description
+ * UsersController controller.
+ */
+ /* @ngInject */
+ function UsersController($rootScope, $scope, USERS) {
+ var vm = this;
+ var rootScopeBroadcastUser = {
+ name: 'Snake',
+ lastName: 'Eyes',
+ city: 'classified'
+ };
+ var scopeBroadcastUser = {
+ name: 'Shana',
+ lastName: 'M. O\'Hara',
+ city: 'Atlanta'
+ };
+
+ vm.getRootScopeBroadcast = function () {
+ $rootScope.$broadcast(USERS.ROOTSCOPE.BROADCAST, rootScopeBroadcastUser);
+ };
+ vm.getScopeBroadcast = function () {
+ $scope.$broadcast(USERS.ROOTSCOPE.BROADCAST, scopeBroadcastUser);
+ };
+
+ $scope.$on(USERS.SCOPE.EMIT_FACT, usersOnEmitFact);
+
+ function usersOnEmitFact(events, emitFact) {
+ vm.emitFact = emitFact;
+ console.log('usersOnEmitFact, events.name: ' + events.name);
+ }
+ }
+
+})();
--- /dev/null
+describe('app.users', function() {
+ 'use strict';
+
+ var $rootScope;
+ var $scope;
+ var USERS;
+ var UsersController;
+
+ beforeEach(function() {
+ module('app.users');
+
+ inject(function($controller, _$rootScope_, _USERS_) {
+ $rootScope = _$rootScope_;
+ USERS = _USERS_;
+ $scope = $rootScope.$new();
+
+ jasmine.createSpy($rootScope, '$rootScope.$broadcast');
+ jasmine.createSpy($scope, '$scope.$broadcast');
+ UsersController = $controller('UsersController', {
+ $rootScope: $rootScope,
+ $scope: $scope,
+ USERS: _USERS_
+ });
+ });
+ });
+
+ describe('UsersController controller', function () {
+
+ it('should be created successfully', function () {
+ expect(UsersController).toBeDefined();
+ });
+
+ it('should be called $rootScope.$broadcast', function () {
+ var rootScopeBroadcastUser = {
+ name: 'Snake',
+ lastName: 'Eyes',
+ city: 'classified'
+ };
+
+ UsersController.getRootScopeBroadcast();
+
+ expect($rootScope.$broadcast).toHaveBeenCalledWith(USERS.ROOTSCOPE.BROADCAST, rootScopeBroadcastUser);
+ });
+
+ it('should be called $scope.$broadcast', function () {
+ var scopeBroadcastUser = {
+ name: 'Shana',
+ lastName: 'M. O\'Hara',
+ city: 'Atlanta'
+ };
+
+ UsersController.getScopeBroadcast();
+
+ expect($scope.$broadcast).toHaveBeenCalledWith(USERS.ROOTSCOPE.BROADCAST, scopeBroadcastUser);
+ });
+ });
+
+});
--- /dev/null
+<!DOCTYPE html>
+<div class="container" ng-controller="UsersController as vm">
+ <div>title = {{vm.emitFact.title}}</div>
+ <div>fact = {{vm.emitFact.fact}}</div>
+ <button type="button" class="btn btn-primary" ng-click="vm.getRootScopeBroadcast()">
+ $rootScope.$broadcast
+ </button>
+
+ <button type="button" class="btn btn-primary" ng-click="vm.getScopeBroadcast()">
+ $scope.$broadcast
+ </button>
+
+ <div class="container" ng-controller="UsersChildController as vm">
+ <div>name = {{vm.broadcastUser.name}}</div>
+ <div>lastname = {{vm.broadcastUser.lastName}}</div>
+ <div>city = {{vm.broadcastUser.city}}</div>
+
+ <button type="button" class="btn btn-primary" ng-click="vm.getEmit()">
+ $scope.$emit fact
+ </button>
+ </div>
+
+</div>
--- /dev/null
+(function() {
+ 'use strict';
+
+ /**
+ * @ngdoc overview
+ * @name app.users
+ *
+ * @requires app.core
+ *
+ * @description
+ * # app.users
+ *
+ * ## Module users.
+ * Module in charge of dealing with users.
+ */
+ angular.module('app.users', [
+ 'app.core'
+ ]);
+
+})();
--- /dev/null
+(function() {
+ 'use strict';
+
+ angular
+ .module('app.users')
+ .config(route);
+
+ /**
+ * @ngdoc service
+ * @name app.users.route
+ *
+ * @requires $stateProvider
+ * @requires $urlRouterProvider
+ *
+ * <p>
+ * <br>
+ * {@link http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateProvider $stateProvider} <br>
+ * {@link http://angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouterProvider $urlRouterProvider}
+ * </p>
+ *
+ *
+ * @description
+ * Router configuration for users application.
+ */
+ /* @ngInject */
+ function route($stateProvider, $urlRouterProvider) {
+ var state = 'users';
+ var config = {
+ abstract: false,
+ url: '/users',
+ templateUrl: 'app/users/users.html'
+ };
+
+ $urlRouterProvider.otherwise(state);
+ $stateProvider.state(state, config);
+ }
+}());
--- /dev/null
+describe('app.users', function() {
+ 'use strict';
+
+ describe('state', function() {
+ var view = {
+ users: 'app/users/users.html'
+ };
+ var $state;
+
+ beforeEach(function() {
+ module('app.users');
+
+ inject(function(_$state_) {
+ $state = _$state_;
+ });
+ });
+
+ it('should map /users route to users View template', function() {
+ expect($state.get('users').templateUrl).toEqual(view.users);
+ });
+
+ });
+
+});
<script src="/src/showcase/app/welcome/welcome.module.js"></script>
<script src="/src/showcase/app/welcome/welcome.route.js"></script>
<script src="/src/showcase/app/welcome/welcome.controller.js"></script>
+ <script src="/src/showcase/app/users/users.module.js"></script>
+ <script src="/src/showcase/app/users/users.route.js"></script>
+ <script src="/src/showcase/app/users/users.controller.js"></script>
+ <script src="/src/showcase/app/users/users.constants.js"></script>
+ <script src="/src/showcase/app/users/users-child.controller.js"></script>
<script src="/src/showcase/app/cars/cars.module.js"></script>
<script src="/src/showcase/app/cars/cars.service.js"></script>
<script src="/src/showcase/app/cars/cars.route.js"></script>