Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Preload references in refrenced_list entries #688

Merged
merged 3 commits into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"url": "git://github.com/marmelab/ng-admin.git"
},
"devDependencies": {
"admin-config": "^0.2.18",
"admin-config": "^0.3.0",
"angular": "~1.3.15",
"angular-bootstrap": "^0.12.0",
"angular-mocks": "1.3.14",
Expand Down
1 change: 1 addition & 0 deletions src/javascripts/ng-admin/Crud/CrudModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
28 changes: 7 additions & 21 deletions src/javascripts/ng-admin/Crud/button/maExportToCsvButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
28 changes: 28 additions & 0 deletions src/javascripts/ng-admin/Crud/column/maReferenceColumn.js
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

===

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming that the APIs we fetch are consistent (i.e. that the type of comment.post_id is the same as the type of post.id) is pretty brave, but I think we'd rather be tolerant to ease compatibility. So e.g. when comment.post_id is '1' and post.id is 1, the current code works.

.pop();
}
},
template: '<ma-column field="::targetField" entry="::referencedEntry" entity="::targetEntity" datastore="::datastore()"></ma-column>'
};
}

maReferenceColumn.$inject = [];

export default maReferenceColumn;
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ function maReferencedListColumn(NgAdminConfiguration) {
<ma-datagrid name="{{ field.datagridName() }}"
entries="::entries"
fields="::field.targetFields()"
list-actions="::field.listActions()"
entity="::entity">
list-actions="::field.listActions()"
entity="::entity"
datastore="::datastore()">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we retrieve entries from the datastore now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got my response IRL: we can't remove entries, as this is the only way to keep entry ids.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, but we'd need to pass an array of ids and a type to let the datagrid retrieve the entries from the datastore... not ideal.

</ma-datagrid>`
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
getReadWidget: () => '<ma-string-column value="::entry.listValues[field.name()]"></ma-string-column>',
getReadWidget: () => '<ma-reference-column field="::field" value="::entry.values[field.name()]" datastore="::datastore"></ma-reference-column>',
getLinkWidget: () => '<a ng-click="gotoReference()">' + module.exports.getReadWidget() + '</a>',
getFilterWidget: () => '<ma-reference-field field="::field" value="values[field.name()]" datastore="::datastore"></ma-reference-field>',
getWriteWidget: () => '<ma-reference-field field="::field" value="entry.values[field.name()]" datastore="::datastore"></ma-reference-field>'
Expand Down
3 changes: 2 additions & 1 deletion src/javascripts/ng-admin/Crud/list/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
selection="selection"
fields="::listController.fields"
list-actions="::listController.listActions"
entity="::listController.entity">
entity="::listController.entity"
datastore="listController.dataStore">
</ma-datagrid>
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions src/javascripts/ng-admin/Crud/list/maDatagrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ define(function (require) {
selection: '=',
fields: '&',
listActions: '&',
entity: '&'
entity: '&',
datastore: '&'
},
controllerAs: 'datagrid',
controller: maDatagridController,
Expand Down Expand Up @@ -45,7 +46,7 @@ define(function (require) {
<ma-datagrid-item-selector toggle-select="toggleSelect(entry)" selection="selection" entry="entry"/>
</td>
<td ng-repeat="field in fields() track by $index" ng-class="field.getCssClasses(entry)" class="ng-admin-column-{{ ::field.name() }} ng-admin-type-{{ ::field.type() }}">
<ma-column field="::field" entry="::entry" entity="::entity"></ma-column>
<ma-column field="::field" entry="::entry" entity="::entity" datastore="datagrid.datastore"></ma-column>
</td>
<td ng-if="datagrid.shouldDisplayActions" class="ng-admin-column-actions">
<ma-list-actions entry="::entry" entity="::entity" buttons="listActions()"></ma-list-actions>
Expand Down
1 change: 1 addition & 0 deletions src/javascripts/ng-admin/Crud/list/maDatagridController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading