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

Commit

Permalink
fix(expressions): change model reference in string hideExpression and…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
kwypchlo committed Feb 18, 2016
1 parent eae422f commit e68c568
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/directives/formly-form.js
Expand Up @@ -328,6 +328,8 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
return originalExpression(...args)
}
watchExpression.displayName = `Formly Watch Expression for field for ${field.key}`
} else if (field.model) {
watchExpression = $parse(watchExpression).bind(null, $scope, {model: field.model})
}
return watchExpression
}
Expand Down Expand Up @@ -366,6 +368,9 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
function getFormlyFieldLikeLocals(field, index) {
// this makes it closer to what a regular formlyExpression would be
return {
// when field.model is not yet an object, this call should be evaluated in $scope.model context
// because this is a call where field.model gets resolved
model: angular.isObject(field.model) ? field.model : $scope.model,
options: field,
index,
formState: $scope.options.formState,
Expand Down
8 changes: 6 additions & 2 deletions src/directives/formly-form.test.js
Expand Up @@ -597,17 +597,21 @@ describe('formly-form', () => {
})

it('ensures that hideExpression has all the expressionProperties values', () => {
scope.model = {nested: {foo: 'bar', baz: []}}
scope.options = {formState: {}}
scope.fields = [{
template: input,
key: 'test',
model: 'model.nested',
hideExpression: `
model === options.data.model &&
options === options.data.field &&
index === 0 &&
formState === options.data.formOptions.formState &&
originalModel === options.data.originalModel &&
formOptions === options.data.formOptions`,
data: {
model: scope.model.nested,
originalModel: scope.model,
formOptions: scope.options,
},
Expand Down Expand Up @@ -1196,7 +1200,7 @@ describe('formly-form', () => {
}
scope.fields[0].model = model
scope.fields[0].watcher = [{
expression: 'model.nested.foo',
expression: 'model.foo',
runFieldExpressions: true,
}]
scope.fields[0].expressionProperties = {
Expand Down Expand Up @@ -1363,7 +1367,7 @@ describe('formly-form', () => {
const field = scope.fields[0]

field.model = 'model.baz'
field.hideExpression = 'model.baz.buzz === "bar"'
field.hideExpression = 'model.buzz === "bar"'

compileAndDigest()
$timeout.flush()
Expand Down

0 comments on commit e68c568

Please sign in to comment.