Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit c899ee6

Browse files
committed
fix(expressions): change model reference in string hideExpression and watcher expression for nested models
BREAKING CHANGE: reference to 'model' in string hideExpressions and watchers on fields with nested models now points to field.model just as in expressionProperties
1 parent eae422f commit c899ee6

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/directives/formly-form.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
4242
${getHideDirective()}="!field.hide"
4343
class="formly-field"
4444
options="field"
45-
model="field.model || model"
45+
model="field.model"
4646
original-model="model"
4747
fields="fields"
4848
form="theFormlyForm"
@@ -265,6 +265,8 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
265265

266266
isNewModel = !referencesCurrentlyWatchedModel(expression)
267267

268+
// temporary assign $scope.model as field.model to evaluate the expression in correct context
269+
field.model = $scope.model
268270
field.model = evalCloseToFormlyExpression(expression, undefined, field, index)
269271
if (!field.model) {
270272
throw formlyUsability.getFieldError(
@@ -273,6 +275,8 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
273275
' expression must have been initialized ahead of time.',
274276
field)
275277
}
278+
} else if (!field.model) {
279+
field.model = $scope.model
276280
}
277281
return isNewModel
278282
}
@@ -328,6 +332,8 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
328332
return originalExpression(...args)
329333
}
330334
watchExpression.displayName = `Formly Watch Expression for field for ${field.key}`
335+
} else if (field.model) {
336+
watchExpression = $parse(watchExpression).bind(null, $scope, {model: field.model})
331337
}
332338
return watchExpression
333339
}
@@ -366,6 +372,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
366372
function getFormlyFieldLikeLocals(field, index) {
367373
// this makes it closer to what a regular formlyExpression would be
368374
return {
375+
model: field.model,
369376
options: field,
370377
index,
371378
formState: $scope.options.formState,

src/directives/formly-form.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,17 +597,21 @@ describe('formly-form', () => {
597597
})
598598

599599
it('ensures that hideExpression has all the expressionProperties values', () => {
600+
scope.model = {nested: {foo: 'bar', baz: []}}
600601
scope.options = {formState: {}}
601602
scope.fields = [{
602603
template: input,
603604
key: 'test',
605+
model: 'model.nested',
604606
hideExpression: `
607+
model === options.data.model &&
605608
options === options.data.field &&
606609
index === 0 &&
607610
formState === options.data.formOptions.formState &&
608611
originalModel === options.data.originalModel &&
609612
formOptions === options.data.formOptions`,
610613
data: {
614+
model: scope.model.nested,
611615
originalModel: scope.model,
612616
formOptions: scope.options,
613617
},
@@ -1196,7 +1200,7 @@ describe('formly-form', () => {
11961200
}
11971201
scope.fields[0].model = model
11981202
scope.fields[0].watcher = [{
1199-
expression: 'model.nested.foo',
1203+
expression: 'model.foo',
12001204
runFieldExpressions: true,
12011205
}]
12021206
scope.fields[0].expressionProperties = {
@@ -1363,7 +1367,7 @@ describe('formly-form', () => {
13631367
const field = scope.fields[0]
13641368

13651369
field.model = 'model.baz'
1366-
field.hideExpression = 'model.baz.buzz === "bar"'
1370+
field.hideExpression = 'model.buzz === "bar"'
13671371

13681372
compileAndDigest()
13691373
$timeout.flush()

0 commit comments

Comments
 (0)