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

Commit 61f891d

Browse files
committedMar 10, 2016
fix(model): keep track of correct model reference
1 parent 9413842 commit 61f891d

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed
 

Diff for: ‎src/directives/formly-form.js

+4-5
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"
45+
model="field.model || model"
4646
original-model="model"
4747
fields="fields"
4848
form="theFormlyForm"
@@ -140,7 +140,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
140140
const promise = field.runExpressions && field.runExpressions()
141141
if (field.hideExpression) { // can't use hide with expressionProperties reliably
142142
const val = model[field.key]
143-
field.hide = evalCloseToFormlyExpression(field.hideExpression, val, field, index)
143+
field.hide = evalCloseToFormlyExpression(field.hideExpression, val, field, index, {model})
144144
}
145145
if (field.extras && field.extras.validateOnModelChange && field.formControl) {
146146
if (angular.isArray(field.formControl)) {
@@ -261,7 +261,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
261261
const model = field.model || $scope.model
262262
$scope.$watch(function hideExpressionWatcher() {
263263
const val = model[field.key]
264-
return evalCloseToFormlyExpression(field.hideExpression, val, field, index)
264+
return evalCloseToFormlyExpression(field.hideExpression, val, field, index, {model})
265265
}, (hide) => field.hide = hide, true)
266266
}
267267
}
@@ -277,9 +277,8 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
277277
field.model = resolveStringModel(expression)
278278

279279
$scope.$watch(() => resolveStringModel(expression), (model) => field.model = model)
280-
} else if (!field.model) {
281-
field.model = $scope.model
282280
}
281+
283282
return isNewModel
284283

285284
function resolveStringModel(expression) {

Diff for: ‎src/directives/formly-form.test.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ describe('formly-form', () => {
445445
{template: input, key: 'foo', model: scope.fieldModel1},
446446
{template: input, key: 'bar', model: scope.fieldModel1},
447447
{template: input, key: 'zoo', model: scope.fieldModel1},
448+
{template: input, key: 'test'},
448449
]
449450
})
450451

@@ -462,6 +463,21 @@ describe('formly-form', () => {
462463
expect(spy1).to.have.been.calledOnce
463464
expect(spy2).to.have.been.calledOnce
464465
})
466+
467+
it('should be updated when the reference to the model changes', () => {
468+
scope.model = {test: 'bar'}
469+
scope.fields[3].expressionProperties = {'data.test': 'model.test'}
470+
471+
compileAndDigest()
472+
$timeout.flush()
473+
474+
scope.model = {test: 'baz'}
475+
476+
scope.$digest()
477+
$timeout.flush()
478+
479+
expect(scope.fields[3].data.test).to.equal('baz')
480+
})
465481
})
466482

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

502519
compileAndDigest()
503520
$timeout.flush()
@@ -509,8 +526,9 @@ describe('formly-form', () => {
509526
}
510527

511528
scope.$digest()
529+
$timeout.flush()
512530

513-
expect(scope.fields[0].model.foo).to.equal('baz')
531+
expect(scope.fields[0].data.foo).to.equal('baz')
514532
})
515533

516534
function testModelAccessor(accessor) {

0 commit comments

Comments
 (0)