Commit f2fcb297 authored by Robert Knight's avatar Robert Knight

Remove usage of bindings in component constructors

In Angular 1.6 the behavior where inputs to components ("bindings")
were accessible in the controller's constructor was deprecated [1] but a
switch was made available to re-enable the old behavior to assist migration.
This switch has been removed in Angular 1.7, so we need to stop relying
on it before we can upgrade.

The correct thing to do is to put all init logic which accesses bindings
(`this.someBindingName`) in the `$onInit` lifecycle method.

This commit makes the minimal amount of changes to enable the app and
tests to run without requiring pre-assigned bindings.

[1] https://toddmotto.com/angular-1-6-is-here
parent b75882bf
...@@ -33,9 +33,11 @@ function AnnotationViewerContentController ( ...@@ -33,9 +33,11 @@ function AnnotationViewerContentController (
const id = $routeParams.id; const id = $routeParams.id;
this.$onInit = () => {
this.search.update = function (query) { this.search.update = function (query) {
$location.path('/stream').search('q', query); $location.path('/stream').search('q', query);
}; };
};
store.subscribe(function () { store.subscribe(function () {
self.rootThread = rootThread.thread(store.getState()); self.rootThread = rootThread.thread(store.getState());
......
...@@ -99,7 +99,7 @@ function AnnotationController( ...@@ -99,7 +99,7 @@ function AnnotationController(
* All initialization code except for assigning the controller instance's * All initialization code except for assigning the controller instance's
* methods goes here. * methods goes here.
*/ */
function init() { this.$onInit = () => {
/** Determines whether controls to expand/collapse the annotation body /** Determines whether controls to expand/collapse the annotation body
* are displayed adjacent to the tags field. * are displayed adjacent to the tags field.
*/ */
...@@ -156,7 +156,7 @@ function AnnotationController( ...@@ -156,7 +156,7 @@ function AnnotationController(
self.edit(); self.edit();
} }
} }
} };
/** Save this annotation if it's a new highlight. /** Save this annotation if it's a new highlight.
* *
...@@ -569,8 +569,6 @@ function AnnotationController( ...@@ -569,8 +569,6 @@ function AnnotationController(
} }
return self.group().type !== 'private'; return self.group().type !== 'private';
}; };
init();
} }
module.exports = { module.exports = {
......
...@@ -202,10 +202,12 @@ function SidebarContentController( ...@@ -202,10 +202,12 @@ function SidebarContentController(
streamer.connect(); streamer.connect();
}); });
this.$onInit = () => {
// If the user is logged in, we connect nevertheless // If the user is logged in, we connect nevertheless
if (this.auth.status === 'logged-in') { if (this.auth.status === 'logged-in') {
streamer.connect(); streamer.connect();
} }
};
$scope.$on(events.USER_CHANGED, function () { $scope.$on(events.USER_CHANGED, function () {
streamer.reconnect(); streamer.reconnect();
......
...@@ -52,15 +52,6 @@ function StreamContentController( ...@@ -52,15 +52,6 @@ function StreamContentController(
store.setForceVisible(id, true); store.setForceVisible(id, true);
}; };
Object.assign(this.search, {
query: function () {
return $routeParams.q || '';
},
update: function (q) {
$location.search({q: q});
},
});
store.subscribe(function () { store.subscribe(function () {
self.rootThread = rootThread.thread(store.getState()); self.rootThread = rootThread.thread(store.getState());
}); });
...@@ -69,6 +60,11 @@ function StreamContentController( ...@@ -69,6 +60,11 @@ function StreamContentController(
store.setSortKey('Newest'); store.setSortKey('Newest');
this.loadMore = fetch; this.loadMore = fetch;
this.$onInit = () => {
this.search.query = () => $routeParams.q || '';
this.search.update = q => $location.search({ q });
};
} }
module.exports = { module.exports = {
......
...@@ -16,10 +16,12 @@ const icons = { ...@@ -16,10 +16,12 @@ const icons = {
// @ngInject // @ngInject
function SvgIconController($element) { function SvgIconController($element) {
this.$onInit = () => {
if (!icons[this.name]) { if (!icons[this.name]) {
throw new Error('Unknown icon: ' + this.name); throw new Error('Unknown icon: ' + this.name);
} }
$element[0].innerHTML = icons[this.name]; $element[0].innerHTML = icons[this.name];
};
} }
module.exports = { module.exports = {
......
...@@ -151,8 +151,7 @@ describe('annotation', function() { ...@@ -151,8 +151,7 @@ describe('annotation', function() {
}) })
.component('markdown', { .component('markdown', {
bindings: require('../markdown').bindings, bindings: require('../markdown').bindings,
}) });
.config(($compileProvider) => $compileProvider.preAssignBindingsEnabled(true));
}); });
beforeEach(angular.mock.module('h')); beforeEach(angular.mock.module('h'));
......
...@@ -35,8 +35,7 @@ describe('annotationViewerContent', function () { ...@@ -35,8 +35,7 @@ describe('annotationViewerContent', function () {
before(function () { before(function () {
angular.module('h', []) angular.module('h', [])
.component('annotationViewerContent', .component('annotationViewerContent',
require('../annotation-viewer-content')) require('../annotation-viewer-content'));
.config(($compileProvider) => $compileProvider.preAssignBindingsEnabled(true));
}); });
beforeEach(angular.mock.module('h')); beforeEach(angular.mock.module('h'));
......
...@@ -57,8 +57,7 @@ describe('sidebar.components.hypothesis-app', function () { ...@@ -57,8 +57,7 @@ describe('sidebar.components.hypothesis-app', function () {
})); }));
angular.module('h', []) angular.module('h', [])
.component('hypothesisApp', component) .component('hypothesisApp', component);
.config(($compileProvider) => $compileProvider.preAssignBindingsEnabled(true));
}); });
beforeEach(angular.mock.module('h')); beforeEach(angular.mock.module('h'));
......
...@@ -67,8 +67,7 @@ describe('sidebar.components.sidebar-content', function () { ...@@ -67,8 +67,7 @@ describe('sidebar.components.sidebar-content', function () {
angular: angular, angular: angular,
'../search-client': FakeSearchClient, '../search-client': FakeSearchClient,
}) })
)) ));
.config(($compileProvider) => $compileProvider.preAssignBindingsEnabled(true));
}); });
beforeEach(angular.mock.module('h')); beforeEach(angular.mock.module('h'));
......
...@@ -26,8 +26,7 @@ describe('StreamContentController', function () { ...@@ -26,8 +26,7 @@ describe('StreamContentController', function () {
before(function () { before(function () {
angular.module('h', []) angular.module('h', [])
.component('streamContent', require('../stream-content')) .component('streamContent', require('../stream-content'));
.config(($compileProvider) => $compileProvider.preAssignBindingsEnabled(true));
}); });
beforeEach(function () { beforeEach(function () {
......
...@@ -7,8 +7,7 @@ const util = require('../../directive/test/util'); ...@@ -7,8 +7,7 @@ const util = require('../../directive/test/util');
describe('svgIcon', function () { describe('svgIcon', function () {
before(function () { before(function () {
angular.module('app', []) angular.module('app', [])
.component('svgIcon', require('../svg-icon')) .component('svgIcon', require('../svg-icon'));
.config(($compileProvider) => $compileProvider.preAssignBindingsEnabled(true));
}); });
beforeEach(function () { beforeEach(function () {
......
...@@ -115,8 +115,7 @@ describe('threadList', function () { ...@@ -115,8 +115,7 @@ describe('threadList', function () {
before(function () { before(function () {
angular.module('app', []) angular.module('app', [])
.component('threadList', threadList) .component('threadList', threadList);
.config(($compileProvider) => $compileProvider.preAssignBindingsEnabled(true));
}); });
beforeEach(function () { beforeEach(function () {
......
...@@ -70,8 +70,10 @@ function ThreadListController($element, $scope, settings, VirtualThreadList) { ...@@ -70,8 +70,10 @@ function ThreadListController($element, $scope, settings, VirtualThreadList) {
const options = Object.assign({ const options = Object.assign({
scrollRoot: this.scrollRoot, scrollRoot: this.scrollRoot,
}, virtualThreadOptions); }, virtualThreadOptions);
let visibleThreads;
const visibleThreads = new VirtualThreadList($scope, window, this.thread, options); this.$onInit = () => {
visibleThreads = new VirtualThreadList($scope, window, this.thread, options);
visibleThreads.on('changed', function (state) { visibleThreads.on('changed', function (state) {
self.virtualThreadList = { self.virtualThreadList = {
visibleThreads: state.visibleThreads, visibleThreads: state.visibleThreads,
...@@ -90,6 +92,7 @@ function ThreadListController($element, $scope, settings, VirtualThreadList) { ...@@ -90,6 +92,7 @@ function ThreadListController($element, $scope, settings, VirtualThreadList) {
}); });
}, 50); }, 50);
}); });
};
/** /**
* Return the vertical scroll offset for the document in order to position the * Return the vertical scroll offset for the document in order to position the
...@@ -134,7 +137,7 @@ function ThreadListController($element, $scope, settings, VirtualThreadList) { ...@@ -134,7 +137,7 @@ function ThreadListController($element, $scope, settings, VirtualThreadList) {
}); });
this.$onChanges = function (changes) { this.$onChanges = function (changes) {
if (changes.thread) { if (changes.thread && visibleThreads) {
visibleThreads.setRootThread(changes.thread.currentValue); visibleThreads.setRootThread(changes.thread.currentValue);
} }
}; };
......
...@@ -95,19 +95,6 @@ function configureToastr(toastrConfig) { ...@@ -95,19 +95,6 @@ function configureToastr(toastrConfig) {
}); });
} }
// @ngInject
function configureCompile($compileProvider) {
// Make component bindings available in controller constructor. When
// pre-assigned bindings is off, as it is by default in Angular >= 1.6.0,
// bindings are only available during and after the controller's `$onInit`
// method.
//
// This migration helper is being removed in Angular 1.7.0. To see which
// components need updating, look for uses of `preAssignBindingsEnabled` in
// tests.
$compileProvider.preAssignBindingsEnabled(true);
}
// @ngInject // @ngInject
function setupHttp($http, streamer) { function setupHttp($http, streamer) {
$http.defaults.headers.common['X-Client-Id'] = streamer.clientId; $http.defaults.headers.common['X-Client-Id'] = streamer.clientId;
...@@ -213,7 +200,6 @@ function startAngularApp(config) { ...@@ -213,7 +200,6 @@ function startAngularApp(config) {
.value('time', require('./util/time')) .value('time', require('./util/time'))
.value('urlEncodeFilter', require('./filter/url').encode) .value('urlEncodeFilter', require('./filter/url').encode)
.config(configureCompile)
.config(configureLocation) .config(configureLocation)
.config(configureRoutes) .config(configureRoutes)
.config(configureToastr) .config(configureToastr)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment