From: Gustavo Martin Morcuende Date: Sat, 19 Sep 2015 15:14:53 +0000 (+0200) Subject: showcase: controller as, accessing to parent controller from child ones X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=ba3a4cc65ba3c01aa2e017fd7745bdd2b21d68b4;p=JavaScriptForFun showcase: controller as, accessing to parent controller from child ones It requires changing the names of controllers in HTML from something common (vm) to something particular. --- diff --git a/angularjs/showcase/src/showcase/app/users/users-child.controller.js b/angularjs/showcase/src/showcase/app/users/users-child.controller.js index 10c3045..bb1836c 100644 --- a/angularjs/showcase/src/showcase/app/users/users-child.controller.js +++ b/angularjs/showcase/src/showcase/app/users/users-child.controller.js @@ -24,6 +24,7 @@ /* @ngInject */ function UsersChildController($rootScope, $scope, USERS) { var vm = this; + var emitFact = { title: 'Snake and Scarlett', fact: 'it is canon' @@ -39,6 +40,12 @@ city: 'UserChild' }; + // This is the right way for accessing to a parent controller from a child one when using the "Controller as" way. + // Problem: we need to name our controllers with something different to the "vm standard", otherwise + // here we wouldn't be able to access to the parent controller because it would have the same name as our child + // controller (vm). So, we need different names for parent and child controllers. + vm.valueForChildControllers = $scope.usersController.toBeCalledFromChildControllers(); + vm.getEmit = function () { $scope.$emit(USERS.SCOPE.EMIT_FACT, emitFact); }; diff --git a/angularjs/showcase/src/showcase/app/users/users-child.controller.spec.js b/angularjs/showcase/src/showcase/app/users/users-child.controller.spec.js index d253c6b..2b5faeb 100644 --- a/angularjs/showcase/src/showcase/app/users/users-child.controller.spec.js +++ b/angularjs/showcase/src/showcase/app/users/users-child.controller.spec.js @@ -1,6 +1,15 @@ describe('app.users', function() { 'use strict'; + var valueForChildControllers = { + variable: 'This is a variable from UsersController', + value: 'Hello child controller' + }; + var usersController = { + toBeCalledFromChildControllers: function() { + return valueForChildControllers; + } + }; var $rootScope; var $scope; var USERS; @@ -14,9 +23,12 @@ describe('app.users', function() { USERS = _USERS_; $scope = $rootScope.$new(); + $scope.usersController = usersController; spyOn($scope, '$emit'); spyOn($rootScope, '$broadcast'); jasmine.createSpy($scope, '$scope.$broadcast'); + spyOn($scope.usersController, 'toBeCalledFromChildControllers') + .and.callThrough(); UsersChildController = $controller('UsersChildController', { $rootScope: $rootScope, $scope: $scope, @@ -29,6 +41,9 @@ describe('app.users', function() { it('should be created successfully', function () { expect(UsersChildController).toBeDefined(); + + expect($scope.usersController.toBeCalledFromChildControllers).toHaveBeenCalled(); + expect(UsersChildController.valueForChildControllers).toEqual(valueForChildControllers); }); it('should be called $scope.$emit', function () { diff --git a/angularjs/showcase/src/showcase/app/users/users.controller.js b/angularjs/showcase/src/showcase/app/users/users.controller.js index e6086ca..7a8723b 100644 --- a/angularjs/showcase/src/showcase/app/users/users.controller.js +++ b/angularjs/showcase/src/showcase/app/users/users.controller.js @@ -34,6 +34,10 @@ lastName: 'M. O\'Hara', city: 'Atlanta' }; + var valueForChildControllers = { + variable: 'This is a variable from UsersController', + value: 'Hello child controller' + }; vm.getRootScopeBroadcast = function () { $rootScope.$broadcast(USERS.ROOTSCOPE.BROADCAST, rootScopeBroadcastUser); @@ -45,6 +49,9 @@ vm.emitFact = emitFact; console.log('usersOnEmitFact, events.name: ' + events.name); }; + vm.toBeCalledFromChildControllers = function () { + return valueForChildControllers; + }; $scope.$on(USERS.SCOPE.EMIT_FACT, vm.usersOnEmitFact); } diff --git a/angularjs/showcase/src/showcase/app/users/users.html b/angularjs/showcase/src/showcase/app/users/users.html index 4627e0c..fba9854 100644 --- a/angularjs/showcase/src/showcase/app/users/users.html +++ b/angularjs/showcase/src/showcase/app/users/users.html @@ -1,42 +1,47 @@ -
-
title = {{vm.emitFact.title}}
-
fact = {{vm.emitFact.fact}}
- - -
-
name = {{vm.broadcastUser.name}}
-
lastname = {{vm.broadcastUser.lastName}}
-
city = {{vm.broadcastUser.city}}
+
+
name = {{usersChildController.broadcastUser.name}}
+
lastname = {{usersChildController.broadcastUser.lastName}}
+
city = {{usersChildController.broadcastUser.city}}
-
- +

+ +
+
valueForChildControllers.variable = {{usersChildController.valueForChildControllers.variable}}
+
valueForChildControllers.value = {{usersChildController.valueForChildControllers.value}}
-
+


-
name = {{vm.rootScopeBroadcastUser.name}}
-
lastname = {{vm.rootScopeBroadcastUser.lastName}}
-
city = {{vm.rootScopeBroadcastUser.city}}
+
name = {{usersSecondChildController.rootScopeBroadcastUser.name}}
+
lastname = {{usersSecondChildController.rootScopeBroadcastUser.lastName}}
+
city = {{usersSecondChildController.rootScopeBroadcastUser.city}}


-
name = {{vm.scopeBroadcastUser.name}}
-
lastname = {{vm.scopeBroadcastUser.lastName}}
-
city = {{vm.scopeBroadcastUser.city}}
+
name = {{usersSecondChildController.scopeBroadcastUser.name}}
+
lastname = {{usersSecondChildController.scopeBroadcastUser.lastName}}
+
city = {{usersSecondChildController.scopeBroadcastUser.city}}