ng-admin
ng-admin copied to clipboard
Impossible to extend reference_many field
Using ng-admin alpha7.
It doesn't seem to be possible to extend a reference_many field.
I copied the ReferenceManyField.js and ReferenceManyFieldView.js, renamed them, and renamed the class in it. I then registered my new field as tag_reference_many.
When creating a field of this type 'tag_reference_many' it does render, but no labels are added in the displayed 'select multiple':
nga.field('categories', 'tag_reference_many')
.label('Kategorier')
.targetEntity(categories)
.targetField(nga.field('navn'))
.validation({required: true})
If I create the field with the above, but just as a 'reference_many' it is working as it should, displaying the labels.
When creating the field definition, I did nothing more than copying the reference_many field type/field view files and renaming the field._type in them.
The problem seems to be this piece of code:
getReferences(fields, withRemoteComplete, optimized = null) {
let references = fields.filter(f => f.type() === 'reference' || f.type() === 'reference_many');
from the file Utils/ReferenceExtractor.js in admin-config.
There is a pull request (https://github.com/marmelab/admin-config/pull/74) that should fix this, but it does not work for me.
I managed to inherit both reference and reference_many fields. If you want the new Field to keep functioning like the reference_many field, you must not override the _type in the child class. Some of the logic in the code is too coupled to the reference fields. Since you should not override the _type, you dont need to register a view. Instead set the template in the constructor of your child field class.