Add logic to unset watchers using scope on destroy

This commit is contained in:
Greg Considine
2017-03-14 13:22:47 -04:00
parent 5d9f27db62
commit d4addb83df
2 changed files with 22 additions and 14 deletions

View File

@@ -7,7 +7,7 @@
import jobSubCredListController from './job-sub-cred-list.controller'; import jobSubCredListController from './job-sub-cred-list.controller';
export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$compile', 'CredentialList', export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$compile', 'CredentialList',
function(templateUrl, qs, GetBasePath, GenerateList, $compile, CredentialList) { (templateUrl, qs, GetBasePath, GenerateList, $compile, CredentialList) => {
return { return {
scope: { scope: {
selectedCredential: '=' selectedCredential: '='
@@ -15,7 +15,9 @@ export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$com
templateUrl: templateUrl('job-submission/lists/credential/job-sub-cred-list'), templateUrl: templateUrl('job-submission/lists/credential/job-sub-cred-list'),
controller: jobSubCredListController, controller: jobSubCredListController,
restrict: 'E', restrict: 'E',
link: function(scope) { link: scope => {
let toDestroy = [];
scope.credential_default_params = { scope.credential_default_params = {
order_by: 'name', order_by: 'name',
page_size: 5, page_size: 5,
@@ -30,11 +32,11 @@ export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$com
// Fire off the initial search // Fire off the initial search
qs.search(GetBasePath('credentials'), scope.credential_default_params) qs.search(GetBasePath('credentials'), scope.credential_default_params)
.then(function(res) { .then(res => {
scope.credential_dataset = res.data; scope.credential_dataset = res.data;
scope.credentials = scope.credential_dataset.results; scope.credentials = scope.credential_dataset.results;
var credList = _.cloneDeep(CredentialList); let credList = _.cloneDeep(CredentialList);
let html = GenerateList.build({ let html = GenerateList.build({
list: credList, list: credList,
input_type: 'radio', input_type: 'radio',
@@ -45,10 +47,10 @@ export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$com
$('#job-submission-credential-lookup').append($compile(html)(scope)); $('#job-submission-credential-lookup').append($compile(html)(scope));
scope.$watchCollection('selectedCredential', function () { toDestroy.push(scope.$watchCollection('selectedCredential', () => {
if(scope.selectedCredential) { if(scope.selectedCredential) {
// Loop across the inventories and see if one of them should be "checked" // Loop across the inventories and see if one of them should be "checked"
scope.credentials.forEach(function(row, i) { scope.credentials.forEach((row, i) => {
if (row.id === scope.selectedCredential.id) { if (row.id === scope.selectedCredential.id) {
scope.credentials[i].checked = 1; scope.credentials[i].checked = 1;
} }
@@ -57,8 +59,10 @@ export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$com
} }
}); });
} }
}); }));
}); });
scope.$on('$destroy', () => toDestroy.forEach(watcher => watcher()));
} }
}; };
}]; }];

View File

@@ -7,7 +7,7 @@
import jobSubInvListController from './job-sub-inv-list.controller'; import jobSubInvListController from './job-sub-inv-list.controller';
export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$compile', 'InventoryList', export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$compile', 'InventoryList',
function(templateUrl, qs, GetBasePath, GenerateList, $compile, InventoryList) { (templateUrl, qs, GetBasePath, GenerateList, $compile, InventoryList) => {
return { return {
scope: { scope: {
selectedInventory: '=' selectedInventory: '='
@@ -15,7 +15,9 @@ export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$com
templateUrl: templateUrl('job-submission/lists/inventory/job-sub-inv-list'), templateUrl: templateUrl('job-submission/lists/inventory/job-sub-inv-list'),
controller: jobSubInvListController, controller: jobSubInvListController,
restrict: 'E', restrict: 'E',
link: function(scope) { link: scope => {
let toDestroy = [];
scope.inventory_default_params = { scope.inventory_default_params = {
order_by: 'name', order_by: 'name',
page_size: 5 page_size: 5
@@ -28,11 +30,11 @@ export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$com
// Fire off the initial search // Fire off the initial search
qs.search(GetBasePath('inventory'), scope.inventory_default_params) qs.search(GetBasePath('inventory'), scope.inventory_default_params)
.then(function(res) { .then(res => {
scope.inventory_dataset = res.data; scope.inventory_dataset = res.data;
scope.inventories = scope.inventory_dataset.results; scope.inventories = scope.inventory_dataset.results;
var invList = _.cloneDeep(InventoryList); let invList = _.cloneDeep(InventoryList);
let html = GenerateList.build({ let html = GenerateList.build({
list: invList, list: invList,
input_type: 'radio', input_type: 'radio',
@@ -43,10 +45,10 @@ export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$com
$('#job-submission-inventory-lookup').append($compile(html)(scope)); $('#job-submission-inventory-lookup').append($compile(html)(scope));
scope.$watchCollection('selectedInventory', function () { toDestroy.push(scope.$watchCollection('selectedInventory', () => {
if(scope.selectedInventory) { if(scope.selectedInventory) {
// Loop across the inventories and see if one of them should be "checked" // Loop across the inventories and see if one of them should be "checked"
scope.inventories.forEach(function(row, i) { scope.inventories.forEach((row, i) => {
if (row.id === scope.selectedInventory.id) { if (row.id === scope.selectedInventory.id) {
scope.inventories[i].checked = 1; scope.inventories[i].checked = 1;
} }
@@ -55,8 +57,10 @@ export default [ 'templateUrl', 'QuerySet', 'GetBasePath', 'generateList', '$com
} }
}); });
} }
}); }));
}); });
scope.$on('$destory', () => toDestroy.forEach(watcher => watcher()));
} }
}; };
}]; }];