ng-admin icon indicating copy to clipboard operation
ng-admin copied to clipboard

Impossible to extend reference_many field

Open gartner opened this issue 8 years ago • 2 comments

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.

gartner avatar Oct 10 '16 07:10 gartner

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.

gartner avatar Oct 11 '16 05:10 gartner

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.

faboulaws avatar Dec 08 '16 20:12 faboulaws