Skip to content

Commit

Permalink
Fix ReferenceField two-step population
Browse files Browse the repository at this point in the history
As specified in #1213, ReferenceField are no longer populated.
This PR aims to fix the issue.
  • Loading branch information
Kmaschta committed Sep 29, 2016
1 parent f35c2ce commit 49790d2
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/javascripts/ng-admin/Crud/field/maChoiceField.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,33 @@ export default function maChoiceField($compile) {
itemsFilter = '';
}

var choices = (typeof scope.choices == 'function' && scope.choices()) ? scope.choices() : (field.choices ? field.choices() : []);
const choices = scope.choices ? scope.choices : field.choices ? field.choices() : [];
let choicesFactory;

if (typeof choices === 'function' && choices(scope.entry)) {
choicesFactory = choices;
scope.choices = choicesFactory(scope.entry);
} else {
scope.choices = choices ? choices : [];
}

var template = `
<ui-select ng-model="$parent.value" ng-required="v.required" id="{{ name }}" name="{{ name }}">
<ui-select-match allow-clear="{{ !v.required }}" placeholder="{{ placeholder | translate }}">{{ $select.selected.label | translate }}</ui-select-match>
<ui-select-choices ${refreshAttributes} repeat="item.value as item in choices ${itemsFilter} track by $index">
<ui-select-choices ${refreshAttributes} repeat="item.value as item in choices ${itemsFilter} track by $index">
{{ item.label | translate }}
</ui-select-choices>
</ui-select>`;

// as choices may be a function depending of another entry field, we need to watch the whole entry
scope.$watch('entry', (newEntry, oldEntry) => {
if (typeof(choices) !== 'function') {
scope.choices = choices;
if (!choicesFactory) {
return;
}

scope.choices = choices(newEntry);
if (!angular.equals(scope.choices, choices(oldEntry))) {
const oldChoices = scope.choices;
scope.choices = choicesFactory(newEntry);
if (!angular.equals(scope.choices, oldChoices)) {
scope.value = null;
}
}, true);
Expand Down

0 comments on commit 49790d2

Please sign in to comment.