From 4785a0588dbf86dec0fc512ff71aca392caadbd8 Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Fri, 11 Sep 2015 16:25:21 +0200 Subject: [PATCH 1/3] Preload references in refrenced_list entries --- package.json | 2 +- src/javascripts/ng-admin/Crud/CrudModule.js | 1 + .../ng-admin/Crud/column/maReferenceColumn.js | 28 +++++++ .../Crud/column/maReferencedListColumn.js | 5 +- .../Crud/fieldView/ReferenceFieldView.js | 2 +- src/javascripts/ng-admin/Crud/list/list.html | 3 +- .../ng-admin/Crud/list/maDatagrid.js | 5 +- .../Crud/list/maDatagridController.js | 1 + src/javascripts/ng-admin/Crud/routing.js | 82 ++++++++++++++++++- .../controller/DashboardController.js | 5 +- .../component/directive/maDashboardPanel.js | 26 +++--- .../ng-admin/Main/view/dashboard.html | 2 +- 12 files changed, 137 insertions(+), 25 deletions(-) create mode 100644 src/javascripts/ng-admin/Crud/column/maReferenceColumn.js diff --git a/package.json b/package.json index 28c66ab7..02c59c5c 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "url": "git://github.com/marmelab/ng-admin.git" }, "devDependencies": { - "admin-config": "^0.2.18", + "admin-config": "^0.2.19", "angular": "~1.3.15", "angular-bootstrap": "^0.12.0", "angular-mocks": "1.3.14", diff --git a/src/javascripts/ng-admin/Crud/CrudModule.js b/src/javascripts/ng-admin/Crud/CrudModule.js index b6bf5689..c45d0729 100644 --- a/src/javascripts/ng-admin/Crud/CrudModule.js +++ b/src/javascripts/ng-admin/Crud/CrudModule.js @@ -55,6 +55,7 @@ CrudModule.directive('maChoicesColumn', require('./column/maChoicesColumn')); CrudModule.directive('maDateColumn', require('./column/maDateColumn')); CrudModule.directive('maJsonColumn', require('./column/maJsonColumn')); CrudModule.directive('maNumberColumn', require('./column/maNumberColumn')); +CrudModule.directive('maReferenceColumn', require('./column/maReferenceColumn')); CrudModule.directive('maReferencedListColumn', require('./column/maReferencedListColumn')); CrudModule.directive('maReferenceManyColumn', require('./column/maReferenceManyColumn')); CrudModule.directive('maReferenceManyLinkColumn', require('./column/maReferenceManyLinkColumn')); diff --git a/src/javascripts/ng-admin/Crud/column/maReferenceColumn.js b/src/javascripts/ng-admin/Crud/column/maReferenceColumn.js new file mode 100644 index 00000000..42267173 --- /dev/null +++ b/src/javascripts/ng-admin/Crud/column/maReferenceColumn.js @@ -0,0 +1,28 @@ +function maReferenceColumn() { + return { + restrict: 'E', + scope: { + field: '&', + value: '&', + datastore: '&' + }, + link: { + pre: function(scope) { + const value = scope.value(); + scope.field = scope.field(); + scope.targetEntity = scope.field.targetEntity(); + scope.targetField = scope.field.targetField(); + const identifierName = scope.targetEntity.identifier().name() + scope.referencedEntry = scope.datastore() + .getEntries(scope.targetEntity.uniqueId + '_values') + .filter(entry => entry.values[identifierName] == value) + .pop(); + } + }, + template: '' + }; +} + +maReferenceColumn.$inject = []; + +export default maReferenceColumn; diff --git a/src/javascripts/ng-admin/Crud/column/maReferencedListColumn.js b/src/javascripts/ng-admin/Crud/column/maReferencedListColumn.js index 2ee15787..69820eff 100644 --- a/src/javascripts/ng-admin/Crud/column/maReferencedListColumn.js +++ b/src/javascripts/ng-admin/Crud/column/maReferencedListColumn.js @@ -17,8 +17,9 @@ function maReferencedListColumn(NgAdminConfiguration) { + list-actions="::field.listActions()" + entity="::entity" + datastore="::datastore()"> ` }; } diff --git a/src/javascripts/ng-admin/Crud/fieldView/ReferenceFieldView.js b/src/javascripts/ng-admin/Crud/fieldView/ReferenceFieldView.js index 781ac7af..0985c743 100644 --- a/src/javascripts/ng-admin/Crud/fieldView/ReferenceFieldView.js +++ b/src/javascripts/ng-admin/Crud/fieldView/ReferenceFieldView.js @@ -1,5 +1,5 @@ module.exports = { - getReadWidget: () => '', + getReadWidget: () => '', getLinkWidget: () => '' + module.exports.getReadWidget() + '', getFilterWidget: () => '', getWriteWidget: () => '' diff --git a/src/javascripts/ng-admin/Crud/list/list.html b/src/javascripts/ng-admin/Crud/list/list.html index e2595a8d..b0ec32c2 100644 --- a/src/javascripts/ng-admin/Crud/list/list.html +++ b/src/javascripts/ng-admin/Crud/list/list.html @@ -5,7 +5,8 @@ selection="selection" fields="::listController.fields" list-actions="::listController.listActions" - entity="::listController.entity"> + entity="::listController.entity" + datastore="listController.dataStore"> diff --git a/src/javascripts/ng-admin/Crud/list/maDatagrid.js b/src/javascripts/ng-admin/Crud/list/maDatagrid.js index 36f0bc7f..ba4b5620 100644 --- a/src/javascripts/ng-admin/Crud/list/maDatagrid.js +++ b/src/javascripts/ng-admin/Crud/list/maDatagrid.js @@ -14,7 +14,8 @@ define(function (require) { selection: '=', fields: '&', listActions: '&', - entity: '&' + entity: '&', + datastore: '&' }, controllerAs: 'datagrid', controller: maDatagridController, @@ -45,7 +46,7 @@ define(function (require) { - + diff --git a/src/javascripts/ng-admin/Crud/list/maDatagridController.js b/src/javascripts/ng-admin/Crud/list/maDatagridController.js index 2ca7e1d0..04020fbf 100644 --- a/src/javascripts/ng-admin/Crud/list/maDatagridController.js +++ b/src/javascripts/ng-admin/Crud/list/maDatagridController.js @@ -17,6 +17,7 @@ define(function () { this.$scope = $scope; this.$location = $location; this.$anchorScroll = $anchorScroll; + this.datastore = this.$scope.datastore(); this.filters = {}; this.shouldDisplayActions = this.$scope.listActions() && this.$scope.listActions().length > 0; diff --git a/src/javascripts/ng-admin/Crud/routing.js b/src/javascripts/ng-admin/Crud/routing.js index 807700f0..84abd3cc 100644 --- a/src/javascripts/ng-admin/Crud/routing.js +++ b/src/javascripts/ng-admin/Crud/routing.js @@ -201,14 +201,14 @@ function routing($stateProvider) { referencedListEntries: ['dataStore', 'view', 'referencedListData', function (dataStore, view, referencedListData) { var referencedLists = view.getReferencedLists(); var referencedList; - var referencedListEntries; + var referencedListRestEntries; for (var i in referencedLists) { referencedList = referencedLists[i]; - referencedListEntries = referencedListData[i]; + referencedListRestEntries = referencedListData[i]; Entry.createArrayFromRest( - referencedListEntries, + referencedListRestEntries, referencedList.targetFields(), referencedList.targetEntity().name(), referencedList.targetEntity().identifier().name() @@ -220,6 +220,44 @@ function routing($stateProvider) { dataStore.addEntry(view.getEntity().uniqueId, entry); + return true; + }], + nonOptimizedReferencedDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { + const referencedLists = view.getReferencedLists(); + var promises = {}; + Object.keys(referencedLists).map(name => { + const nonOptimizedReferences = referencedLists[name].getNonOptimizedReferences(); + if (Object.keys(nonOptimizedReferences).length === 0) return; + promises[name] = ReadQueries.getFilteredReferenceData(nonOptimizedReferences, referencedListData[name]); + }); + return $q.all(promises) + }], + optimizedReferencedDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { + const referencedLists = view.getReferencedLists(); + var promises = {}; + Object.keys(referencedLists).map(name => { + const optimizedReferences = referencedLists[name].getOptimizedReferences(); + if (Object.keys(optimizedReferences).length === 0) return; + promises[name] = ReadQueries.getOptimizedReferencedData(optimizedReferences, referencedListData[name]); + }); + return $q.all(promises) + }], + referencedEntriesForReferencedLists: ['dataStore', 'view', 'nonOptimizedReferencedDataForReferencedLists', 'optimizedReferencedDataForReferencedLists', function(dataStore, view, nonOptimizedReferencedDataForReferencedLists, optimizedReferencedDataForReferencedLists) { + const referencedLists = view.getReferencedLists(); + const referencedData = angular.extend(nonOptimizedReferencedDataForReferencedLists, optimizedReferencedDataForReferencedLists); + Object.keys(referencedLists).map(referencedListName => { + var references = referencedLists[referencedListName].getReferences(); + for (var name in references) { + if (!referencedData[referencedListName][name]) continue; + Entry.createArrayFromRest( + referencedData[referencedListName][name], + [references[name].targetField()], + references[name].targetEntity().name(), + references[name].targetEntity().identifier().name() + ).map(entry => dataStore.addEntry(references[name].targetEntity().uniqueId + '_values', entry)) + } + + }) return true; }] } @@ -374,6 +412,44 @@ function routing($stateProvider) { ); } + return true; + }], + nonOptimizedReferencedDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { + const referencedLists = view.getReferencedLists(); + var promises = {}; + Object.keys(referencedLists).map(name => { + const nonOptimizedReferences = referencedLists[name].getNonOptimizedReferences(); + if (Object.keys(nonOptimizedReferences).length === 0) return; + promises[name] = ReadQueries.getFilteredReferenceData(nonOptimizedReferences, referencedListData[name]); + }); + return $q.all(promises) + }], + optimizedReferencedDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { + const referencedLists = view.getReferencedLists(); + var promises = {}; + Object.keys(referencedLists).map(name => { + const optimizedReferences = referencedLists[name].getOptimizedReferences(); + if (Object.keys(optimizedReferences).length === 0) return; + promises[name] = ReadQueries.getOptimizedReferencedData(optimizedReferences, referencedListData[name]); + }); + return $q.all(promises) + }], + referencedEntriesForReferencedLists: ['dataStore', 'view', 'nonOptimizedReferencedDataForReferencedLists', 'optimizedReferencedDataForReferencedLists', function(dataStore, view, nonOptimizedReferencedDataForReferencedLists, optimizedReferencedDataForReferencedLists) { + const referencedLists = view.getReferencedLists(); + const referencedData = angular.extend(nonOptimizedReferencedDataForReferencedLists, optimizedReferencedDataForReferencedLists); + Object.keys(referencedLists).map(referencedListName => { + var references = referencedLists[referencedListName].getReferences(); + for (var name in references) { + if (!referencedData[referencedListName][name]) continue; + Entry.createArrayFromRest( + referencedData[referencedListName][name], + [references[name].targetField()], + references[name].targetEntity().name(), + references[name].targetEntity().identifier().name() + ).map(entry => dataStore.addEntry(references[name].targetEntity().uniqueId + '_values', entry)) + } + + }) return true; }] } diff --git a/src/javascripts/ng-admin/Main/component/controller/DashboardController.js b/src/javascripts/ng-admin/Main/component/controller/DashboardController.js index edada82d..fadc94a1 100644 --- a/src/javascripts/ng-admin/Main/component/controller/DashboardController.js +++ b/src/javascripts/ng-admin/Main/component/controller/DashboardController.js @@ -10,11 +10,12 @@ define(function (require) { * @param {PanelBuilder} PanelBuilder * @constructor */ - function DashboardController($scope, $state, collections, entries, hasEntities) { + function DashboardController($scope, $state, collections, entries, hasEntities, dataStore) { this.$state = $state; this.collections = collections; this.entries = entries; this.hasEntities = hasEntities; + this.datastore = dataStore; $scope.$on('$destroy', this.destroy.bind(this)); } @@ -27,7 +28,7 @@ define(function (require) { this.$state = undefined; }; - DashboardController.$inject = ['$scope', '$state', 'collections', 'entries', 'hasEntities']; + DashboardController.$inject = ['$scope', '$state', 'collections', 'entries', 'hasEntities', 'dataStore']; return DashboardController; }); diff --git a/src/javascripts/ng-admin/Main/component/directive/maDashboardPanel.js b/src/javascripts/ng-admin/Main/component/directive/maDashboardPanel.js index 57f02073..a6e68f76 100644 --- a/src/javascripts/ng-admin/Main/component/directive/maDashboardPanel.js +++ b/src/javascripts/ng-admin/Main/component/directive/maDashboardPanel.js @@ -3,27 +3,29 @@ function maDashboardPanel($state) { restrict: 'E', scope: { collection: '&', - entries: '&' + entries: '&', + datastore: '&' }, link: function(scope) { scope.gotoList = function () { $state.go($state.get('list'), { entity: scope.collection().entity.name() }); }; }, - template: - '' + - '' + - '' + template: +` + +` }; } maDashboardPanel.$inject = ['$state']; -module.exports = maDashboardPanel; +export default maDashboardPanel; diff --git a/src/javascripts/ng-admin/Main/view/dashboard.html b/src/javascripts/ng-admin/Main/view/dashboard.html index d5a1e81e..b31dd9ac 100644 --- a/src/javascripts/ng-admin/Main/view/dashboard.html +++ b/src/javascripts/ng-admin/Main/view/dashboard.html @@ -14,7 +14,7 @@

Dashboard

- +
From 3d5dda15a2b58d0c07138465e25233a26118a845 Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Wed, 16 Sep 2015 14:28:55 +0200 Subject: [PATCH 2/3] Fix unit test --- .../test/unit/Crud/list/DatagridControllerSpec.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/javascripts/test/unit/Crud/list/DatagridControllerSpec.js b/src/javascripts/test/unit/Crud/list/DatagridControllerSpec.js index 3754064e..98143f61 100644 --- a/src/javascripts/test/unit/Crud/list/DatagridControllerSpec.js +++ b/src/javascripts/test/unit/Crud/list/DatagridControllerSpec.js @@ -14,16 +14,13 @@ describe('controller: ma-datagrid', function () { ]; dataGridController = new DataGridController({ - listActions: function() { - return []; - }, - entity: function () { - return new Entity('my_entity'); - }, + listActions: () => [], + entity: () => new Entity('my_entity'), entries: entries, - selection: [] + selection: [], + datastore: () => { return {}; } }, { - search: function () { + search: () => { return {}; } }, {}); From e9b16e7f89babd06b923e5cebbee83f128c13871 Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Wed, 16 Sep 2015 17:41:55 +0200 Subject: [PATCH 3/3] Refactor for better clarity The call for unoptimized then optimized data is moved to ReadQuery --- package.json | 2 +- .../Crud/button/maExportToCsvButton.js | 28 +-- src/javascripts/ng-admin/Crud/routing.js | 222 +++++------------- .../ng-admin/Main/config/routing.js | 21 +- 4 files changed, 72 insertions(+), 201 deletions(-) diff --git a/package.json b/package.json index 02c59c5c..4b488081 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "url": "git://github.com/marmelab/ng-admin.git" }, "devDependencies": { - "admin-config": "^0.2.19", + "admin-config": "^0.3.0", "angular": "~1.3.15", "angular-bootstrap": "^0.12.0", "angular-mocks": "1.3.14", diff --git a/src/javascripts/ng-admin/Crud/button/maExportToCsvButton.js b/src/javascripts/ng-admin/Crud/button/maExportToCsvButton.js index b99bb0ef..60d59e1d 100644 --- a/src/javascripts/ng-admin/Crud/button/maExportToCsvButton.js +++ b/src/javascripts/ng-admin/Crud/button/maExportToCsvButton.js @@ -40,30 +40,16 @@ define(function () { rawEntries = response.data; return rawEntries; }) - .then(rawEntries => ReadQueries.getFilteredReferenceData(exportView.getNonOptimizedReferences(), rawEntries)) - .then(nonOptimizedReference => { - nonOptimizedReferencedData = nonOptimizedReference; - return ReadQueries.getOptimizedReferencedData(exportView.getOptimizedReferences(), rawEntries); - }) - .then(optimizedReference => { - optimizedReferencedData = optimizedReference; - var references = exportView.getReferences(), - referencedData = angular.extend(nonOptimizedReferencedData, optimizedReferencedData), - referencedEntries; - - for (var name in referencedData) { - referencedEntries = AdminDescription.getEntryConstructor().createArrayFromRest( - referencedData[name], + .then(rawEntries => ReadQueries.getReferenceData(exportView.fields(), rawEntries)) + .then(referenceData => { + const references = exportView.getReferences(); + for (var name in referenceData) { + AdminDescription.getEntryConstructor().createArrayFromRest( + referenceData[name], [references[name].targetField()], references[name].targetEntity().name(), references[name].targetEntity().identifier().name() - - ); - - scope.datastore.setEntries( - references[name].targetEntity().uniqueId + '_values', - referencedEntries - ); + ).map(entry => scope.datastore.addEntry(references[name].targetEntity().uniqueId + '_values', entry)); } }) .then(function () { diff --git a/src/javascripts/ng-admin/Crud/routing.js b/src/javascripts/ng-admin/Crud/routing.js index 84abd3cc..412dfabf 100644 --- a/src/javascripts/ng-admin/Crud/routing.js +++ b/src/javascripts/ng-admin/Crud/routing.js @@ -57,24 +57,15 @@ function routing($stateProvider) { return ReadQueries.getAllReferencedData(view.getFilterReferences(false)); }], filterEntries: ['dataStore', 'view', 'filterData', function (dataStore, view, filterData) { - var filters = view.getFilterReferences(false); - var filterEntries; - + const filters = view.getFilterReferences(false); for (var name in filterData) { - filterEntries = Entry.createArrayFromRest( + Entry.createArrayFromRest( filterData[name], [filters[name].targetField()], filters[name].targetEntity().name(), filters[name].targetEntity().identifier().name() - ); - - dataStore.setEntries( - filters[name].targetEntity().uniqueId + '_choices', - filterEntries - ); + ).map(entry => dataStore.addEntry(filters[name].targetEntity().uniqueId + '_choices', entry)); } - - return true; }] } }) @@ -106,28 +97,21 @@ function routing($stateProvider) { totalItems: ['response', function (response) { return response.totalItems; }], - nonOptimizedReferencedData: ['ReadQueries', 'view', 'response', function (ReadQueries, view, response) { - return ReadQueries.getFilteredReferenceData(view.getNonOptimizedReferences(), response.data); - }], - optimizedReferencedData: ['ReadQueries', 'view', 'response', function (ReadQueries, view, response) { - return ReadQueries.getOptimizedReferencedData(view.getOptimizedReferences(), response.data); + referenceData: ['ReadQueries', 'view', 'response', function (ReadQueries, view, response) { + return ReadQueries.getReferenceData(view.fields(), response.data); }], - referencedEntries: ['dataStore', 'view', 'nonOptimizedReferencedData', 'optimizedReferencedData', function (dataStore, view, nonOptimizedReferencedData, optimizedReferencedData) { - var references = view.getReferences(), - referencedData = angular.extend(nonOptimizedReferencedData, optimizedReferencedData); - - for (var name in referencedData) { + referenceEntries: ['dataStore', 'view', 'referenceData', function (dataStore, view, referenceData) { + const references = view.getReferences(); + for (var name in referenceData) { Entry.createArrayFromRest( - referencedData[name], + referenceData[name], [references[name].targetField()], references[name].targetEntity().name(), references[name].targetEntity().identifier().name() ).map(entry => dataStore.addEntry(references[name].targetEntity().uniqueId + '_values', entry)); } - - return true; }], - entries: ['dataStore', 'view', 'response', 'referencedEntries', function (dataStore, view, response, referencedEntries) { + entries: ['dataStore', 'view', 'response', 'referenceEntries', function (dataStore, view, response, referenceEntries) { var entries = view.mapEntries(response.data); // shortcut to diplay collection of entry with included referenced values @@ -170,93 +154,59 @@ function routing($stateProvider) { entry: ['view', 'rawEntry', function(view, rawEntry) { return view.mapEntry(rawEntry); }], - nonOptimizedReferencedData: ['ReadQueries', 'view', 'entry', function (ReadQueries, view, entry) { - return ReadQueries.getFilteredReferenceData(view.getNonOptimizedReferences(), [entry.values]); + referenceData: ['ReadQueries', 'view', 'entry', function (ReadQueries, view, entry) { + return ReadQueries.getReferenceData(view.fields(), [entry.values]); }], - optimizedReferencedData: ['ReadQueries', 'view', 'entry', function (ReadQueries, view, entry) { - return ReadQueries.getOptimizedReferencedData(view.getOptimizedReferences(), [entry.values]); - }], - referencedEntries: ['dataStore', 'view', 'nonOptimizedReferencedData', 'optimizedReferencedData', function (dataStore, view, nonOptimizedReferencedData, optimizedReferencedData) { - var references = view.getReferences(), - referencedData = angular.extend(nonOptimizedReferencedData, optimizedReferencedData); - - for (var name in referencedData) { + referenceEntries: ['dataStore', 'view', 'referenceData', function (dataStore, view, referenceData) { + const references = view.getReferences(); + for (var name in referenceData) { Entry.createArrayFromRest( - referencedData[name], + referenceData[name], [references[name].targetField()], references[name].targetEntity().name(), references[name].targetEntity().identifier().name() ).map(entry => dataStore.addEntry(references[name].targetEntity().uniqueId + '_values', entry)) } - - return true; }], referencedListData: ['$stateParams', 'ReadQueries', 'view', 'entry', function ($stateParams, ReadQueries, view, entry) { - var referencedLists = view.getReferencedLists(); - var sortField = $stateParams.sortField; - var sortDir = $stateParams.sortDir; - - return ReadQueries.getReferencedListData(referencedLists, sortField, sortDir, entry.identifierValue); + return ReadQueries.getReferencedListData(view.getReferencedLists(), $stateParams.sortField, $stateParams.sortDir, entry.identifierValue); }], referencedListEntries: ['dataStore', 'view', 'referencedListData', function (dataStore, view, referencedListData) { - var referencedLists = view.getReferencedLists(); - var referencedList; - var referencedListRestEntries; - - for (var i in referencedLists) { - referencedList = referencedLists[i]; - referencedListRestEntries = referencedListData[i]; - + const referencedLists = view.getReferencedLists(); + for (var name in referencedLists) { Entry.createArrayFromRest( - referencedListRestEntries, - referencedList.targetFields(), - referencedList.targetEntity().name(), - referencedList.targetEntity().identifier().name() - ).map(entry => dataStore.addEntry(referencedList.targetEntity().uniqueId + '_list', entry)); + referencedListData[name], + referencedLists[name].targetFields(), + referencedLists[name].targetEntity().name(), + referencedLists[name].targetEntity().identifier().name() + ).map(entry => dataStore.addEntry(referencedLists[name].targetEntity().uniqueId + '_list', entry)) } }], - entryWithReferences: ['dataStore', 'view', 'entry', 'referencedEntries', function(dataStore, view, entry, referencedEntries) { + entryWithReferences: ['dataStore', 'view', 'entry', 'referenceEntries', function(dataStore, view, entry, referenceEntries) { dataStore.fillReferencesValuesFromEntry(entry, view.getReferences(), true); - dataStore.addEntry(view.getEntity().uniqueId, entry); - - return true; - }], - nonOptimizedReferencedDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { - const referencedLists = view.getReferencedLists(); - var promises = {}; - Object.keys(referencedLists).map(name => { - const nonOptimizedReferences = referencedLists[name].getNonOptimizedReferences(); - if (Object.keys(nonOptimizedReferences).length === 0) return; - promises[name] = ReadQueries.getFilteredReferenceData(nonOptimizedReferences, referencedListData[name]); - }); - return $q.all(promises) }], - optimizedReferencedDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { + referenceDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { const referencedLists = view.getReferencedLists(); var promises = {}; Object.keys(referencedLists).map(name => { - const optimizedReferences = referencedLists[name].getOptimizedReferences(); - if (Object.keys(optimizedReferences).length === 0) return; - promises[name] = ReadQueries.getOptimizedReferencedData(optimizedReferences, referencedListData[name]); + promises[name] = ReadQueries.getReferenceData(referencedLists[name].targetFields(), referencedListData[name]); }); return $q.all(promises) }], - referencedEntriesForReferencedLists: ['dataStore', 'view', 'nonOptimizedReferencedDataForReferencedLists', 'optimizedReferencedDataForReferencedLists', function(dataStore, view, nonOptimizedReferencedDataForReferencedLists, optimizedReferencedDataForReferencedLists) { + referenceEntriesForReferencedLists: ['dataStore', 'view', 'referenceDataForReferencedLists', function(dataStore, view, referenceDataForReferencedLists) { const referencedLists = view.getReferencedLists(); - const referencedData = angular.extend(nonOptimizedReferencedDataForReferencedLists, optimizedReferencedDataForReferencedLists); Object.keys(referencedLists).map(referencedListName => { - var references = referencedLists[referencedListName].getReferences(); + const references = referencedLists[referencedListName].getReferences(); for (var name in references) { - if (!referencedData[referencedListName][name]) continue; + if (!referenceDataForReferencedLists[referencedListName][name]) continue; Entry.createArrayFromRest( - referencedData[referencedListName][name], + referenceDataForReferencedLists[referencedListName][name], [references[name].targetField()], references[name].targetEntity().name(), references[name].targetEntity().identifier().name() ).map(entry => dataStore.addEntry(references[name].targetEntity().uniqueId + '_values', entry)) } - }) return true; }] @@ -289,24 +239,15 @@ function routing($stateProvider) { return ReadQueries.getAllReferencedData(view.getReferences(false)); }], choiceEntries: ['dataStore', 'view', 'choiceData', function (dataStore, view, filterData) { - var choices = view.getReferences(false); - var choiceEntries; - + const choices = view.getReferences(false); for (var name in filterData) { choiceEntries = Entry.createArrayFromRest( filterData[name], [choices[name].targetField()], choices[name].targetEntity().name(), choices[name].targetEntity().identifier().name() - ); - - dataStore.setEntries( - choices[name].targetEntity().uniqueId + '_choices', - choiceEntries - ); + ).map(entry => dataStore.addEntry(choices[name].targetEntity().uniqueId + '_choices', entry)); } - - return true; }] } }); @@ -335,120 +276,73 @@ function routing($stateProvider) { entry: ['view', 'rawEntry', function(view, rawEntry) { return view.mapEntry(rawEntry); }], - nonOptimizedReferencedData: ['ReadQueries', 'view', 'entry', function (ReadQueries, view, entry) { - return ReadQueries.getFilteredReferenceData(view.getNonOptimizedReferences(), [entry.values]); - }], - optimizedReferencedData: ['ReadQueries', 'view', 'entry', function (ReadQueries, view, entry) { - return ReadQueries.getOptimizedReferencedData(view.getOptimizedReferences(), [entry.values]); + referenceData: ['ReadQueries', 'view', 'entry', function (ReadQueries, view, entry) { + return ReadQueries.getReferenceData(view.fields(), [entry.values]); }], - referencedEntries: ['dataStore', 'view', 'nonOptimizedReferencedData', 'optimizedReferencedData', function (dataStore, view, nonOptimizedReferencedData, optimizedReferencedData) { - var references = view.getReferences(), - referencedData = angular.extend(nonOptimizedReferencedData, optimizedReferencedData); - - for (var name in referencedData) { + referenceEntries: ['dataStore', 'view', 'referenceData', function (dataStore, view, referenceData) { + const references = view.getReferences(); + for (var name in referenceData) { Entry.createArrayFromRest( - referencedData[name], + referenceData[name], [references[name].targetField()], references[name].targetEntity().name(), references[name].targetEntity().identifier().name() ).map(entry => dataStore.addEntry(references[name].targetEntity().uniqueId + '_values', entry)) } - - return true; }], referencedListData: ['$stateParams', 'ReadQueries', 'view', 'entry', function ($stateParams, ReadQueries, view, entry) { - var referencedLists = view.getReferencedLists(); - var sortField = $stateParams.sortField; - var sortDir = $stateParams.sortDir; - - return ReadQueries.getReferencedListData(referencedLists, sortField, sortDir, entry.identifierValue); + return ReadQueries.getReferencedListData(view.getReferencedLists(), $stateParams.sortField, $stateParams.sortDir, entry.identifierValue); }], referencedListEntries: ['dataStore', 'view', 'referencedListData', function (dataStore, view, referencedListData) { - var referencedLists = view.getReferencedLists(); - var referencedList; - var referencedListEntries; - - for (var i in referencedLists) { - referencedList = referencedLists[i]; - referencedListEntries = referencedListData[i]; - - referencedListEntries = Entry.createArrayFromRest( - referencedListEntries, - referencedList.targetFields(), - referencedList.targetEntity().name(), - referencedList.targetEntity().identifier().name() - ); - - dataStore.setEntries( - referencedList.targetEntity().uniqueId + '_list', - referencedListEntries - ); + const referencedLists = view.getReferencedLists(); + for (var name in referencedLists) { + Entry.createArrayFromRest( + referencedListData[name], + referencedLists[name].targetFields(), + referencedLists[name].targetEntity().name(), + referencedLists[name].targetEntity().identifier().name() + ).map(entry => dataStore.addEntry(referencedLists[name].targetEntity().uniqueId + '_list', entry)) } }], - entryWithReferences: ['dataStore', 'view', 'entry', 'referencedEntries', function(dataStore, view, entry, referencedEntries) { + entryWithReferences: ['dataStore', 'view', 'entry', 'referenceEntries', function(dataStore, view, entry, referenceEntries) { dataStore.fillReferencesValuesFromEntry(entry, view.getReferences(), true); - dataStore.addEntry(view.getEntity().uniqueId, entry); - return true; }], choiceData: ['ReadQueries', 'view', function (ReadQueries, view) { return ReadQueries.getAllReferencedData(view.getReferences(false)); }], choiceEntries: ['dataStore', 'view', 'choiceData', function (dataStore, view, filterData) { - var choices = view.getReferences(false); - var choiceEntries; - + const choices = view.getReferences(false); for (var name in filterData) { - choiceEntries = Entry.createArrayFromRest( + Entry.createArrayFromRest( filterData[name], [choices[name].targetField()], choices[name].targetEntity().name(), choices[name].targetEntity().identifier().name() - ); - - dataStore.setEntries( - choices[name].targetEntity().uniqueId + '_choices', - choiceEntries - ); + ).map(entry => dataStore.addEntry(choices[name].targetEntity().uniqueId + '_choices', entry)); } - - return true; }], - nonOptimizedReferencedDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { + referenceDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { const referencedLists = view.getReferencedLists(); var promises = {}; Object.keys(referencedLists).map(name => { - const nonOptimizedReferences = referencedLists[name].getNonOptimizedReferences(); - if (Object.keys(nonOptimizedReferences).length === 0) return; - promises[name] = ReadQueries.getFilteredReferenceData(nonOptimizedReferences, referencedListData[name]); + promises[name] = ReadQueries.getReferenceData(referencedLists[name].targetFields(), referencedListData[name]); }); return $q.all(promises) }], - optimizedReferencedDataForReferencedLists: ['$q', 'ReadQueries', 'view', 'referencedListData', function ($q,ReadQueries, view, referencedListData) { + referenceEntriesForReferencedLists: ['dataStore', 'view', 'referenceDataForReferencedLists', function(dataStore, view, referenceDataForReferencedLists) { const referencedLists = view.getReferencedLists(); - var promises = {}; - Object.keys(referencedLists).map(name => { - const optimizedReferences = referencedLists[name].getOptimizedReferences(); - if (Object.keys(optimizedReferences).length === 0) return; - promises[name] = ReadQueries.getOptimizedReferencedData(optimizedReferences, referencedListData[name]); - }); - return $q.all(promises) - }], - referencedEntriesForReferencedLists: ['dataStore', 'view', 'nonOptimizedReferencedDataForReferencedLists', 'optimizedReferencedDataForReferencedLists', function(dataStore, view, nonOptimizedReferencedDataForReferencedLists, optimizedReferencedDataForReferencedLists) { - const referencedLists = view.getReferencedLists(); - const referencedData = angular.extend(nonOptimizedReferencedDataForReferencedLists, optimizedReferencedDataForReferencedLists); Object.keys(referencedLists).map(referencedListName => { - var references = referencedLists[referencedListName].getReferences(); + const references = referencedLists[referencedListName].getReferences(); for (var name in references) { - if (!referencedData[referencedListName][name]) continue; + if (!referenceDataForReferencedLists[referencedListName][name]) continue; Entry.createArrayFromRest( - referencedData[referencedListName][name], + referenceDataForReferencedLists[referencedListName][name], [references[name].targetField()], references[name].targetEntity().name(), references[name].targetEntity().identifier().name() ).map(entry => dataStore.addEntry(references[name].targetEntity().uniqueId + '_values', entry)) } - }) return true; }] diff --git a/src/javascripts/ng-admin/Main/config/routing.js b/src/javascripts/ng-admin/Main/config/routing.js index 229b5931..949843e7 100644 --- a/src/javascripts/ng-admin/Main/config/routing.js +++ b/src/javascripts/ng-admin/Main/config/routing.js @@ -65,7 +65,7 @@ function routing($stateProvider, $urlRouterProvider) { collectionSortDir = sortDir; } promises[collectionName] = (function (collection, collectionSortField, collectionSortDir) { - var rawEntries, nonOptimizedReferencedData, optimizedReferencedData; + var rawEntries; return ReadQueries .getAll(collection, 1, {}, collectionSortField, collectionSortDir) @@ -73,21 +73,12 @@ function routing($stateProvider, $urlRouterProvider) { rawEntries = response.data; return rawEntries; }) - .then(rawEntries => ReadQueries.getFilteredReferenceData(collection.getNonOptimizedReferences(), rawEntries) - ) - .then(nonOptimizedReference => { - nonOptimizedReferencedData = nonOptimizedReference; - return ReadQueries.getOptimizedReferencedData(collection.getOptimizedReferences(), rawEntries); - }) - .then(optimizedReference => { - optimizedReferencedData = optimizedReference; - - var references = collection.getReferences(), - referencedData = angular.extend(nonOptimizedReferencedData, optimizedReferencedData); - - for (var name in referencedData) { + .then(rawEntries => ReadQueries.getReferenceData(collection.fields(), rawEntries)) + .then(referenceData => { + const references = collection.getReferences(); + for (var name in referenceData) { Entry.createArrayFromRest( - referencedData[name], + referenceData[name], [references[name].targetField()], references[name].targetEntity().name(), references[name].targetEntity().identifier().name()