1
0
Fork 0

dapi: Fix *_label setting

The `*_label' fields were not being properly set because they were being
considered just as every other expanded field, which is subject to other
considerations to ensure that we do not overwrite user data.  Label fields,
on the other hand, _must_ always be set whenever the value changes, and are
_not_ modifiable by the user.

This codifies that `_label' assumption that I would like removed in the
future, but time does not permit that sort of refactoring right
now.  Ideally, labels would be treated generically like any other expanded
field and have an attribute set of them that would change their expansion
behavior.

* src/client/dapi/DataApiMediator.js (_updateFieldData): Pass label field id
    to `_populateWithMap'.
  (_populateWithMap): Use it to always populate the label field when the key
    matches, ignoring the other expansion criteria that would inhibit it.
* test/client/dapi/DataApiMediatorTest.js: Update test accordingly.
* src/dapi/DataApiManager.js (triggerFieldUpdate): Provide id to
    `_genUiFieldData'.
  (_genUiFieldData)[label_id]: New field.  Return it as part of the return
    object's `label_id' field.

DEV-4077
master
Mike Gerwitz 2018-12-17 13:25:51 -05:00
parent 68ecb3536d
commit 6b3ab89e77
3 changed files with 39 additions and 13 deletions

View File

@ -178,8 +178,10 @@ module.exports = Class( 'DataApiMediator',
group.setOptions( name, i, val_label, existing[ i ] )
);
const { label_id = "" } = val_label[ 0 ] || {};
const update = this._populateWithMap(
dapi_manager, name, indexes, quote
dapi_manager, name, indexes, quote, label_id
);
update[ name ] = field_update;
@ -196,16 +198,19 @@ module.exports = Class( 'DataApiMediator',
* expansion data are missing, then the field will be ignored. If a
* destination field is populated such that auto-expanding would
* override that datum, then that field will be excluded from the
* expansion.
* expansion. Labels are exempt from this rule, since they are
* considered to be married to the value; labels are not
* user-modifiable.
*
* @param {DataApiManager} dapi_manager manager responsible for fields
* @param {string} name field name
* @param {Array<number>} indexes field indexes
* @param {Quote} quote source quote
* @param {string} label_id name of label field
*
* @return {undefined}
*/
'private _populateWithMap'( dapi_manager, name, indexes, quote )
'private _populateWithMap'( dapi_manager, name, indexes, quote, label_id )
{
const map = this._dapi_map[ name ];
@ -249,7 +254,7 @@ module.exports = Class( 'DataApiMediator',
// if set and non-empty, then it's already populated and we
// must leave the value alone (so as not to override
// something the user directly entered)
if ( existing !== undefined && existing !== "" )
if ( key !== label_id && existing !== undefined && existing !== "" )
{
return;
}

View File

@ -451,11 +451,18 @@ module.exports = Class( 'DataApiManager' )
fdepth[ index ]++;
// TODO: remove this assumption
const label_id = name + '_label';
try
{
this._fieldDataEmitted[ name ][ index ] = true;
this.emit( 'updateFieldData',
name, index, this._genUiFieldData( data, value, label ), fdata
this.emit(
'updateFieldData',
name,
index,
this._genUiFieldData( data, value, label, label_id ),
fdata
);
}
catch ( e )
@ -499,13 +506,14 @@ module.exports = Class( 'DataApiManager' )
*
* This data is ideal for updating a UI and contains no extra information.
*
* @param {Array.<Object>} data return data
* @param {string} value param to map to value
* @param {string} label param to map to label
* @param {Array.<Object>} data return data
* @param {string} value param to map to value
* @param {string} label param to map to label
* @param {string} label_id label field id
*
* @return {Array.<Object>} value and label data set
*/
'private _genUiFieldData': function( data, value, label )
'private _genUiFieldData': function( data, value, label, label_id )
{
var ret = [],
len = data.length;
@ -515,8 +523,9 @@ module.exports = Class( 'DataApiManager' )
var idata = data[ i ];
ret[ i ] = {
value: idata[ value ],
label: idata[ label ]
value: idata[ value ],
label: idata[ label ],
label_id: label_id,
};
}

View File

@ -290,17 +290,28 @@ describe( "DataApiMediator", () =>
index: 0,
value: {
foo: [ "first", "second" ],
label: [ "populated label" ], // exception to the rule
dest1: [ "leave alone" ],
dest2: [ "" ],
},
expected: {
foo: [ "first" ],
// labels should _always_ be populated, since they're
// considered to be part of the actual value, not a
// user-modifiable destination field
label: [ "first label" ],
// dest1 missing because it is already populated
dest2: [ "src2data" ],
},
val_label: [
{ value: "first result", label: "first" },
{
value: "first result",
label: "first label",
label_id: 'label',
},
],
results: {
@ -311,6 +322,7 @@ describe( "DataApiMediator", () =>
expansion: [ {
dest1: [ "src1data" ],
dest2: [ "src2data" ],
label: [ "first label" ],
} ],
}
].forEach( ( {