Slide 35
Slide 35 text
use strict';
angular.module(‘contentful').controller('FormWidgetsController',
['$scope', ‘$injector’, function($scope, $injector){
// 1. Self-reference
var controller = this;
// 2. requirements
var editingInterfaces = $injector.get('editingInterfaces');
var logger = $injector.get('logger');
// 3. Do scope stuff
// 3a. Set up watchers on the scope.
$scope.$watch(localesWatcher, updateWidgets, true);
$scope.$watch('spaceContext.space.getDefaultLocale()', updateWidgets);
$scope.$watch('preferences.showDisabledFields', updateWidgets);
$scope.$watch('errorPaths', updateWidgets);
// 3b. Expose methods or data on the scope
$scope.widgets = [];
!
// 3c. Listen to events on the scope
// 4. Expose methods and properties on the controller instance
this.updateWidgets = updateWidgets;
this.updateWidgetsFromInterface = updateWidgetsFromInterface;
// 5. Clean up
$scope.$on('$destroy', function(){
// Do whatever cleanup might be necessary
controller = null; // MEMLEAK FIX
$scope = null; // MEMLEAK FIX
});
use strict';
angular.module(‘contentful').controller('FormWidgetsController',
['$scope', ‘$injector’, function($scope, $injector){
// 1. Self-reference
var controller = this;
// 2. requirements
var editingInterfaces = $injector.get('editingInterfaces');
var logger = $injector.get('logger');
// 3. Do scope stuff
// 3a. Set up watchers on the scope.
$scope.$watch(localesWatcher, updateWidgets, true);
$scope.$watch('spaceContext.space.getDefaultLocale()', updateWidgets);
$scope.$watch('preferences.showDisabledFields', updateWidgets);
$scope.$watch('errorPaths', updateWidgets);
// 3b. Expose methods or data on the scope
$scope.widgets = [];
// 3c. Listen to events on the scope
// 4. Expose methods and properties on the controller instance
this.updateWidgets = updateWidgets;
this.updateWidgetsFromInterface = updateWidgetsFromInterface;
// 5. Clean up
$scope.$on('$destroy', function(){
// Do whatever cleanup might be necessary
controller = null; // MEMLEAK FIX
$scope = null; // MEMLEAK FIX
});
Then write your requirements
!
Injector scales better if you have more dependencies:
- Unused services/jshint
- List of parameters does not get too long