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

Commit c09b685

Browse files
author
Kent C. Dodds
committed
feat(defaultOptions): Pass scope to the function version
In order to get access to some critical information about the field, we're passing the to the defaultOptions function in addition to the original . This comes in as the 2nd argument (new).
1 parent 670a3c9 commit c09b685

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

src/directives/formly-field.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function formlyField($http, $q, $compile, $templateCache, $interpolate, formlyCo
131131
function mergeOptions(options, extraOptions) {
132132
if (extraOptions) {
133133
if (angular.isFunction(extraOptions)) {
134-
extraOptions = extraOptions(options);
134+
extraOptions = extraOptions(options, $scope);
135135
}
136136
formlyUtil.reverseDeepMerge(options, extraOptions);
137137
}

src/providers/formlyConfig.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,22 @@ function formlyConfig(formlyUsabilityProvider, formlyErrorAndWarningsUrlPrefix,
154154
const optionsDOIsFn = angular.isFunction(optionsDO);
155155
const extendsDOIsFn = angular.isFunction(extendsDO);
156156
if (extendsDOIsFn) {
157-
options.defaultOptions = function defaultOptions(opts) {
158-
const extendsDefaultOptions = extendsDO(opts);
157+
options.defaultOptions = function defaultOptions(opts, scope) {
158+
const extendsDefaultOptions = extendsDO(opts, scope);
159159
const mergedDefaultOptions = {};
160160
utils.reverseDeepMerge(mergedDefaultOptions, opts, extendsDefaultOptions);
161161
let extenderOptionsDefaultOptions = optionsDO;
162162
if (optionsDOIsFn) {
163-
extenderOptionsDefaultOptions = extenderOptionsDefaultOptions(mergedDefaultOptions);
163+
extenderOptionsDefaultOptions = extenderOptionsDefaultOptions(mergedDefaultOptions, scope);
164164
}
165165
utils.reverseDeepMerge(extendsDefaultOptions, extenderOptionsDefaultOptions);
166166
return extendsDefaultOptions;
167167
};
168168
} else if (optionsDOIsFn) {
169-
options.defaultOptions = function defaultOptions(opts) {
169+
options.defaultOptions = function defaultOptions(opts, scope) {
170170
const newDefaultOptions = {};
171171
utils.reverseDeepMerge(newDefaultOptions, opts, extendsDO);
172-
return optionsDO(newDefaultOptions);
172+
return optionsDO(newDefaultOptions, scope);
173173
};
174174
}
175175
}

src/providers/formlyConfig.test.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,10 @@ describe('formlyConfig', () => {
316316
});
317317

318318
describe(`function cases`, () => {
319-
let args, parentFn, childFn, parentDefaultOptions, childDefaultOptions, argsAndParent;
319+
let args, fakeScope, parentFn, childFn, parentDefaultOptions, childDefaultOptions, argsAndParent;
320320
beforeEach(() => {
321321
args = {data: {someData: true}};
322+
fakeScope = {};
322323
parentDefaultOptions = {
323324
data: {extraOptions: true},
324325
templateOptions: {placeholder: 'hi'}
@@ -339,27 +340,27 @@ describe('formlyConfig', () => {
339340
{name, defaultOptions: parentFn},
340341
{name: 'type2', extends: name, defaultOptions: childFn}
341342
]);
342-
getterFn('type2').defaultOptions(args);
343-
expect(parentFn).to.have.been.calledWith(args);
344-
expect(childFn).to.have.been.calledWith(argsAndParent);
343+
getterFn('type2').defaultOptions(args, fakeScope);
344+
expect(parentFn).to.have.been.calledWith(args, fakeScope);
345+
expect(childFn).to.have.been.calledWith(argsAndParent, fakeScope);
345346
});
346347

347348
it(`should call the extended parent's defaultOptions function when it doesn't have one of its own`, () => {
348349
setterFn([
349350
{name, defaultOptions: parentFn},
350351
{name: 'type2', extends: name}
351352
]);
352-
getterFn('type2').defaultOptions(args);
353-
expect(parentFn).to.have.been.calledWith(args);
353+
getterFn('type2').defaultOptions(args, fakeScope);
354+
expect(parentFn).to.have.been.calledWith(args, fakeScope);
354355
});
355356

356357
it(`should call its own defaultOptions function when the parent doesn't have one`, () => {
357358
setterFn([
358359
{name, template},
359360
{name: 'type2', extends: name, defaultOptions: childFn}
360361
]);
361-
getterFn('type2').defaultOptions(args);
362-
expect(childFn).to.have.been.calledWith(args);
362+
getterFn('type2').defaultOptions(args, fakeScope);
363+
expect(childFn).to.have.been.calledWith(args, fakeScope);
363364
});
364365

365366
it(`should extend its defaultOptions object with the parent's defaultOptions object`, () => {
@@ -379,8 +380,8 @@ describe('formlyConfig', () => {
379380
{name, defaultOptions: parentDefaultOptions},
380381
{name: 'type2', extends: name, defaultOptions: childFn}
381382
]);
382-
const returned = getterFn('type2').defaultOptions(args);
383-
expect(childFn).to.have.been.calledWith(argsAndParent);
383+
const returned = getterFn('type2').defaultOptions(args, fakeScope);
384+
expect(childFn).to.have.been.calledWith(argsAndParent, fakeScope);
384385
expect(returned).to.eql(childDefaultOptions);
385386
});
386387
});

0 commit comments

Comments
 (0)