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

Commit 8c96efa

Browse files
author
Kent C. Dodds
committed
Merge pull request #443 from davincho/master
feat(field): Add `oroginalModel` to fields options. provide access to original model Closes #419
2 parents 89bc1d6 + 2c6693b commit 8c96efa

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/directives/formly-field.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
2020
scope: {
2121
options: '=',
2222
model: '=',
23+
originalModel: '=?',
2324
formId: '@', // TODO remove formId in a breaking release
2425
index: '=?',
2526
fields: '=?',
@@ -84,6 +85,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
8485
function simplifyLife(options) {
8586
// add a few empty objects (if they don't already exist) so you don't have to undefined check everywhere
8687
formlyUtil.reverseDeepMerge(options, {
88+
originalModel: options.model,
8789
extras: {},
8890
data: {},
8991
templateOptions: {},

src/directives/formly-field.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,41 @@ describe('formly-field', function() {
14641464
expect(expressionPropertySpy).to.have.been.calledOnce;
14651465
});
14661466

1467+
it('should make original model available on field scope, even another model has been set for field', () => {
1468+
1469+
scope.model = {foo: 'bar', child: {fox: 'jumps'}};
1470+
1471+
scope.fields = [
1472+
getNewField({key: 'foo '}),
1473+
getNewField({key: 'bar', model: 'model.child'})
1474+
];
1475+
1476+
compileAndDigest();
1477+
1478+
const field1 = getIsolateScope(0);
1479+
const field2 = getIsolateScope(1);
1480+
1481+
expect(field1.model).to.eq(scope.model);
1482+
expect(field1.originalModel).to.eq(field1.model);
1483+
1484+
expect(field2.model).to.eq(scope.model.child);
1485+
expect(field2.originalModel).not.to.eq(scope.model.child);
1486+
expect(field2.originalModel).to.eq(scope.model);
1487+
});
1488+
1489+
it('should take field model as default for original model, if original value attributes has not been set', () => {
1490+
scope.fields = [
1491+
getNewField({key: 'foo', model: {foo: 'bar'}})
1492+
];
1493+
1494+
compileAndDigest('<div formly-field class="formly-field" options="fields[0]" model="model"></div>');
1495+
$timeout.flush();
1496+
1497+
expect(field.model).to.eq(scope.fields[0].model);
1498+
expect(field.originalModel).to.eql(scope.fields[0].model);
1499+
1500+
});
1501+
14671502
});
14681503

14691504
describe(`fieldGroup`, () => {

src/directives/formly-form.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function formlyForm(formlyUsability, formlyWarn, $parse, formlyConfig, $interpol
4343
class="formly-field"
4444
options="field"
4545
model="field.model || model"
46+
original-model="model"
4647
fields="fields"
4748
form="theFormlyForm"
4849
form-id="${getFormName()}"

src/providers/formlyApiCheck.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const fieldOptionsApiShape = {
9898
).optional,
9999
key: apiCheck.oneOfType([apiCheck.string, apiCheck.number]).optional,
100100
model: modelChecker.optional,
101+
originalModel: modelChecker.optional,
101102
className: apiCheck.string.optional,
102103
id: apiCheck.string.optional,
103104
name: apiCheck.string.optional,

0 commit comments

Comments
 (0)