Skip to content

Commit 0969f48

Browse files
committed
fix: Don't use watcher on conversions
1 parent b7ef2a4 commit 0969f48

1 file changed

Lines changed: 31 additions & 23 deletions

File tree

src/fng-ui-select.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@
7474
const promises = value.map((value) => {
7575
const id = value.x ? (value.x.id || value.x) : value; // if it's already been converted, throw away the result of the previous conversion
7676
if (!id) {
77-
return $q.resolve({ data: { list: ""} }); // nothing to convert
77+
return $q.resolve({ data: { list: "" } }); // nothing to convert
7878
} else if (valueCache[id]) {
79-
return $q.resolve({ data: { list: valueCache[id] }}); // already cached
79+
return $q.resolve({ data: { list: valueCache[id] } }); // already cached
8080
} else {
8181
return SubmissionsService.getListAttributes(formSchema.ref, id); // need to look it up
8282
}
@@ -106,7 +106,7 @@
106106
useCacheOrLookItUp(formSchema.ref, value, (text) => { cb(formSchema, { id: value, text }) });
107107
}
108108
},
109-
doOwnConversion: function(scope, processedAttrs, ref) {
109+
doOwnConversion: function (scope, processedAttrs, ref) {
110110
var id = RecordHandlerService.getData(scope.record, processedAttrs.info.name, scope.$index);
111111
if (id) {
112112
id = id.id || id; // in case it's already been converted
@@ -162,7 +162,7 @@
162162
// using an arbitrary number and if so just send the _id (on the assumption that large records won't be new so will have an _id).
163163
// According to https://stackoverflow.com/questions/57431355/how-to-fix-431-request-header-fields-too-large-in-react-redux-app the limit is 8KB
164164
// but I have seen it fail at about 5k...
165-
var record = JSON.stringify($scope.record).length >= (4 * 1024) ? {_id: $scope.record._id} : $scope.record;
165+
var record = JSON.stringify($scope.record).length >= (4 * 1024) ? { _id: $scope.record._id } : $scope.record;
166166
// Custom URL
167167
promise = $http({
168168
method: 'GET',
@@ -191,15 +191,15 @@
191191
}
192192
}
193193
})
194-
.catch(function(err) {
195-
var msg;
196-
if (err && err.data && err.data.message) {
197-
msg = err.data.message;
198-
} else {
199-
msg = 'Error ' + err.status + ': ' + err.statusText + ' - ' + err.data;
200-
}
201-
$scope.showError(msg);
202-
});
194+
.catch(function (err) {
195+
var msg;
196+
if (err && err.data && err.data.message) {
197+
msg = err.data.message;
198+
} else {
199+
msg = 'Error ' + err.status + ': ' + err.statusText + ' - ' + err.data;
200+
}
201+
$scope.showError(msg);
202+
});
203203
}
204204
} else {
205205
throw new Error('Could not find uiSelect element for ' + id);
@@ -224,23 +224,31 @@
224224
function addToConversions(path, options) {
225225
if (Object.keys(options).length > 0) {
226226
var keys = path.split('.');
227-
if (!scope.conversions) {
228-
scope.conversions = {};
227+
228+
// Find the scope that owns the form (where the watcher is)
229+
var conversionScope = scope;
230+
while (conversionScope.$parent && !conversionScope.hasOwnProperty('topLevelFormName')) {
231+
conversionScope = conversionScope.$parent;
232+
}
233+
234+
if (!conversionScope.conversions) {
235+
conversionScope.conversions = {};
229236
}
230-
var target = scope.conversions;
237+
var target = conversionScope.conversions;
231238
for (var i = 0; i < keys.length; i++) {
232239
var thisKey = keys[i];
233240
target[thisKey] = target[thisKey] || {};
234241
target = target[thisKey];
235242
}
236243
angular.extend(target, options);
244+
conversionScope.conversionsModified = true;
237245
}
238246
}
239247

240248
const processedAttrs = PluginHelperService.extractFromAttr(attrs, 'fngUiSelect', scope);
241249
const id = processedAttrs.info.id;
242250
const uniqueId = scope.$index !== undefined ? processedAttrs.info.id + "_" + scope.$index : id;
243-
const elemScope = angular.extend({selectId: uniqueId}, processedAttrs.directiveOptions);
251+
const elemScope = angular.extend({ selectId: uniqueId }, processedAttrs.directiveOptions);
244252
const multi = processedAttrs.info.array;
245253
let elementHtml;
246254
let input = '';
@@ -259,7 +267,7 @@
259267
if (angular.module('ngAnimate')) {
260268
theme = 'select2';
261269
}
262-
} catch(e) {
270+
} catch (e) {
263271
; // module not present, so bootstrap theme will work
264272
}
265273
}
@@ -316,7 +324,7 @@
316324
function optionsFromArray(multiControl, multi, array, arrayGetter) {
317325
var isObjects = scope[array] && (scope[array].isObjects || typeof scope[array][0] === "object");
318326
if (isObjects) {
319-
addToConversions(processedAttrs.info.name, {fngajax: FngUISelectHelperService.lookupFunc});
327+
addToConversions(processedAttrs.info.name, { fngajax: FngUISelectHelperService.lookupFunc });
320328
FngUISelectHelperService.addClientLookup(arrayGetter, scope[array]);
321329
}
322330
var select = '';
@@ -351,7 +359,7 @@
351359
// specifies whether or not the buildingBlocks that it creates should include the id or not.
352360
const idIdx = buildingBlocks.common.indexOf('id="');
353361
if (idIdx > -1) {
354-
const endIdIdx = buildingBlocks.common.indexOf('"', idIdx+4);
362+
const endIdIdx = buildingBlocks.common.indexOf('"', idIdx + 4);
355363
if (endIdIdx > -1) {
356364
buildingBlocks.common = buildingBlocks.common.substring(0, idIdx) + buildingBlocks.common.substring(endIdIdx + 1);
357365
}
@@ -367,7 +375,7 @@
367375
// if we have a hard-coded ref (processedAttrs.info.ref), forms-angular can perform the lookup conversion for us.
368376
// where processedAttrs.directiveOptions.refprop is being used instead, the ref is 'variable' - it comes from a property of scope.record, and
369377
// in this case, we'll need to handle the conversions ourselves.
370-
addToConversions(processedAttrs.info.name, {fngajax: FngUISelectHelperService.lookupFunc, noconvert: !!processedAttrs.directiveOptions.refprop});
378+
addToConversions(processedAttrs.info.name, { fngajax: FngUISelectHelperService.lookupFunc, noconvert: !!processedAttrs.directiveOptions.refprop });
371379
if (processedAttrs.directiveOptions.refprop) {
372380
// the property that we'll be getting the ref from may not be populated yet, and might conceivably change at any time, so we
373381
// need to $watch it. buildingBlocks.modelString will be something like "record.<array>[$index].<field>". replacing <field>
@@ -390,7 +398,7 @@
390398
});
391399
} else {
392400
elemScope.ref = processedAttrs.info.ref;
393-
}
401+
}
394402
scope[`${uniqueId}_options`] = [];
395403
if (multiControl) {
396404
select += '{{$select.selected.text}}';
@@ -420,7 +428,7 @@
420428
select += '</ui-select>';
421429
return select;
422430
}
423-
);
431+
);
424432
element.append($compile(input + elementHtml)(scope));
425433
}
426434
}

0 commit comments

Comments
 (0)