Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/directives/formly-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
${getHideDirective()}="!field.hide"
class="formly-field"
options="field"
model="field.model"
model="field.model || model"
original-model="model"
fields="fields"
form="theFormlyForm"
Expand Down Expand Up @@ -140,7 +140,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
const promise = field.runExpressions && field.runExpressions()
if (field.hideExpression) { // can't use hide with expressionProperties reliably
const val = model[field.key]
field.hide = evalCloseToFormlyExpression(field.hideExpression, val, field, index)
field.hide = evalCloseToFormlyExpression(field.hideExpression, val, field, index, {model})
}
if (field.extras && field.extras.validateOnModelChange && field.formControl) {
if (angular.isArray(field.formControl)) {
Expand Down Expand Up @@ -261,7 +261,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
const model = field.model || $scope.model
$scope.$watch(function hideExpressionWatcher() {
const val = model[field.key]
return evalCloseToFormlyExpression(field.hideExpression, val, field, index)
return evalCloseToFormlyExpression(field.hideExpression, val, field, index, {model})
}, (hide) => field.hide = hide, true)
}
}
Expand All @@ -277,9 +277,8 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
field.model = resolveStringModel(expression)

$scope.$watch(() => resolveStringModel(expression), (model) => field.model = model)
} else if (!field.model) {
field.model = $scope.model
}

return isNewModel

function resolveStringModel(expression) {
Expand Down
20 changes: 19 additions & 1 deletion src/directives/formly-form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ describe('formly-form', () => {
{template: input, key: 'foo', model: scope.fieldModel1},
{template: input, key: 'bar', model: scope.fieldModel1},
{template: input, key: 'zoo', model: scope.fieldModel1},
{template: input, key: 'test'},
]
})

Expand All @@ -462,6 +463,21 @@ describe('formly-form', () => {
expect(spy1).to.have.been.calledOnce
expect(spy2).to.have.been.calledOnce
})

it('should be updated when the reference to the model changes', () => {
scope.model = {test: 'bar'}
scope.fields[3].expressionProperties = {'data.test': 'model.test'}

compileAndDigest()
$timeout.flush()

scope.model = {test: 'baz'}

scope.$digest()
$timeout.flush()

expect(scope.fields[3].data.test).to.equal('baz')
})
})

describe('nested model as string', () => {
Expand Down Expand Up @@ -498,6 +514,7 @@ describe('formly-form', () => {
it('should be updated when the reference to the outer model changes', () => {
scope.model.nested.foo = 'bar'
scope.fields[0].model = 'model.nested'
scope.fields[0].expressionProperties = {'data.foo': 'model.foo'}

compileAndDigest()
$timeout.flush()
Expand All @@ -509,8 +526,9 @@ describe('formly-form', () => {
}

scope.$digest()
$timeout.flush()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can scope.$digest() be removed after adding $timeout.flush()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, $digest is needed to trigger the watcher on formly directive for the model property and $timeout is just a dirty workaround for runExpressions method :)


expect(scope.fields[0].model.foo).to.equal('baz')
expect(scope.fields[0].data.foo).to.equal('baz')
})

function testModelAccessor(accessor) {
Expand Down