AnsweredAssumed Answered

this.model in quote-data-group-list does not have relate field from record.php

Question asked by Richard Coleman on Mar 9, 2018
Latest reply on Mar 12, 2018 by Matt Marum

Hello,

 

I've been struggling to figure this one out and wondered if anyone had any ideas. We have a requirement to make a relationship between products and Tax Rates, selectable in the 7.9+ Sidecar quote-data-group-list.php view.

 

Here's a brief outline of what I've done:

 

I have this vardef in custom/Extension/modules/Products/Ext/Vardefs/file.php

$dictionary['Product']['fields']['vat_rate'] = array(
     'name' => 'vat_rate',
     'vname' => 'LBL_LIST_VAT_RATE',
     'type' => 'id',
     'required'=>false,
     'do_report'=>false,
     'reportable'=>false,
);
$dictionary['Product']['fields']['vat_rate_name'] = array(
    'name' => 'vat_rate_name',
    'rname' => 'name',
    'id_name' => 'vat_rate',
    'join_name' => 'taxrates',
    'type' => 'relate',
    'link' => 'taxrates',
    'table' => 'taxrates',
    'isnull' => 'true',
    'module' => 'TaxRates',
    'dbType' => 'varchar',
    'len' => '255',
    'vname' => 'LBL_LIST_VAT_RATE',
    'source' => 'non-db',
    'comment' => 'Vat Rate Name',
    'massupdate' => false,
);

 

 

I've copied quote-data-group-list.php to custom from modules/Products/clients/base/views/quote-data-group-list.php and added this just before the total_amount array:

array(
    'name' => 'vat_rate_name',
    'type' => 'quote-vat-relate',
    'initial_filter' => 'active_taxrates',
    'filter_populate' => array(
        'module' => array('TaxRates'),
    ),
    'related_fields' => array(
        'vat_rate_name',
        'vat_rate',
        'taxrates'
    ),
),

 

You'll see I have a custom field type called quote-vat-relate, which I've mocked from the product template selector, extending the BaseRelateField type, located at custom/modules/Products/clients/base/fields/quote-vat-relate.js

({
    extendsFrom: 'BaseRelateField',

    format: function(value) {
        var idList;
        value = value || this.model.get('vat_rate_name');

        this._super('format', [value]);

        if (value) {
            idList =  this.model.get('vat_rate');
            if (_.isArray(value)) {
                this.formattedIds = idList.join(this._separator);
            } else {
                this.formattedIds = idList;
            }

            if (_.isEmpty(this.formattedIds)) {
                this.formattedIds = value;
            }
        }

        return value;
    },
})

 

This all seems to be going well as when I edit the items, I can select a VAT code, and I can confirm it stores in the database. Great!

Working

 

Navigate, and come back, and it's disappeared!

 

When I dug into this, I found that when the format() function is initially invoked, this.model doesn't contain my field. Even stranger is that when I move the product lines around, it invokes format() again, and the value suddenly displays! When I looking at the value of this.model now, it contains all of the attributes, including vat_rate_name now!

 

So it's seems to be down to an initial display issue. Can I ensure that this field is included in this.model from the start? Is this a bug?

Any suggestions greatly appreciated.

Outcomes