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

Commit de2835c

Browse files
author
Kent C. Dodds
committed
Adding formlyOptionsProvider to configure global options for formly
1 parent eab1f1a commit de2835c

File tree

8 files changed

+90
-11
lines changed

8 files changed

+90
-11
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
Version numbers correspond to `bower.json` version
22

3+
# 0.0.12
4+
5+
## Features
6+
7+
Adding the `formlyOptionsProvider` which allows developers to set default form options.
8+
9+
## Bug Fixes
10+
11+
## Breaking Changes
12+
313
# 0.0.11
414

515
## Features

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Example data as it would be set in the controller
8181
};
8282
```
8383
### Creating Forms
84-
Forms can be customized with the options below.
84+
Forms can be customized with the options below. Note, you can configure this on a form-by-form basis as shown in the example, or globally using the [`formlyOptionsProvider`](#global-config).
8585

8686
#### uniqueFormId (string, required)
8787
>`uniqueFormId` is used to identify the form.
@@ -400,9 +400,11 @@ _Example password field_
400400

401401
### Global Config
402402

403+
#### formlyTemplateProvider
404+
403405
You can configure formly to use custom templates for specified types (your own "text" template) by injecting the `formlyTemplateProvider` in your app's `config` function. The `formlyTemplateProvider` has the following functions:
404406

405-
#### setTemplateUrl
407+
##### setTemplateUrl
406408

407409
Allows you to set a template
408410

@@ -418,7 +420,7 @@ formlyTemplateProvider.setTemplate({
418420
});
419421
```
420422

421-
#### getTemplateUrl
423+
##### getTemplateUrl
422424

423425
Allows you to get the template
424426

@@ -427,6 +429,30 @@ formlyTemplateProvider.setTemplateUrl('radio', 'views/custom-formly-radio.html')
427429
formlyTemplateProvider.getTemplateUrl('radio') === 'views/custom-formly-radio.html'; // true
428430
```
429431

432+
#### formlyOptionsProvider
433+
434+
You can configure default options for all forms using the `formlyOptionsProvider` in your app's `config` function. This has the following api:
435+
436+
##### setOption
437+
438+
Allows you to set an option
439+
440+
```javascript
441+
formlyOptionsProvider.setOption('uniqueFormId', 'probablyDontWantToDoThis');
442+
formlyOptionsProvider.setOption('submitCopy', 'Save');
443+
444+
// the same can be accomplished with
445+
446+
formlyOptionsProvider.setOption({
447+
submitCopy: 'Save',
448+
uniqueFormId: 'probablyDontWantToDoThis'
449+
});
450+
```
451+
452+
##### getOptions
453+
454+
Returns a copy of the current options. This is used internally.
455+
430456
## Roadmap
431457

432458
## Release Notes

src/app.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var app = angular.module('app', ['ng',
88

99
app.constant('usingCustomTypeTemplates', window.localStorage.getItem('useCustomTypeTemplates') === 'true');
1010

11-
app.config(function($stateProvider, $urlRouterProvider, $locationProvider, formlyTemplateProvider, usingCustomTypeTemplates) {
11+
app.config(function($stateProvider, $urlRouterProvider, $locationProvider, formlyTemplateProvider, formlyOptionsProvider, usingCustomTypeTemplates) {
1212
$locationProvider.html5Mode(false);
1313
$locationProvider.hashPrefix('!');
1414

@@ -28,6 +28,13 @@ app.config(function($stateProvider, $urlRouterProvider, $locationProvider, forml
2828
checkbox: 'views/custom-field-checkbox.html'
2929
});
3030
}
31+
32+
formlyOptionsProvider.setOption('uniqueFormId', 'whyConfigureUniqueId');
33+
// or
34+
formlyOptionsProvider.setOption({
35+
submitCopy: 'Configured Submit',
36+
hideSubmit: true
37+
});
3138
});
3239

3340
app.run(function($rootScope, $state, $stateParams, $window) {

src/directives/formly-form.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
angular.module('formly.render')
3-
.directive('formlyForm', function formlyForm() {
3+
.directive('formlyForm', function formlyForm(formlyOptions) {
44
return {
55
restrict: 'AE',
66
templateUrl: 'directives/formly-form.html',
@@ -21,6 +21,7 @@ angular.module('formly.render')
2121
}
2222
},
2323
controller: function($scope, $element, $parse) {
24+
$scope.options = angular.extend(formlyOptions.getOptions(), $scope.options);
2425
$scope.$watch('result', function(newValue) {
2526
angular.forEach($scope.fields, function(field, index) {
2627
if (field.hideExpression) {

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141
<script src="directives/formly-form.js"></script>
4242
<script src="directives/formly-field.js"></script>
4343
<script src="providers/formly-template.js"></script>
44+
<script src="providers/formly-options.js"></script>
4445
</body>
4546
</html>

src/providers/formly-options.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
angular.module('formly.render')
3+
.provider('formlyOptions', function() {
4+
5+
var options = {
6+
uniqueFormId: null,
7+
submitCopy: "Submit",
8+
hideSubmit: false
9+
};
10+
11+
function setOption(name, value) {
12+
if (typeof name === 'string') {
13+
options[name] = value;
14+
} else {
15+
angular.forEach(name, function(val, name) {
16+
setOption(name, val);
17+
});
18+
}
19+
}
20+
21+
function getOptions() {
22+
// copy to avoid third-parties manipulating the options outside of the api.
23+
return angular.copy(options);
24+
}
25+
26+
this.setOption = setOption;
27+
this.getOptions = getOptions;
28+
this.$get = function formlyOptions() {
29+
return this;
30+
}
31+
32+
});

src/views/home.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ <h3>Form State</h3>
106106
</tbody>
107107

108108
</table>
109+
110+
<h3>Configured Default Options</h3>
111+
<div hljs source="preConfiguredOptions|json"></div>
109112
</div>
110113
</div>
111114
</div>

src/views/home.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
app.controller('home', function($scope, $parse, $rootScope, $window, usingCustomTypeTemplates) {
2+
app.controller('home', function($scope, $parse, formlyOptions, $window, usingCustomTypeTemplates) {
33
// Public Methods
44
$scope.onSubmit = function onSubmit() {
55
$scope.submittedData = $scope.formData;
@@ -29,8 +29,6 @@ app.controller('home', function($scope, $parse, $rootScope, $window, usingCustom
2929
$scope.formFields = $parse(newValue)({});
3030
$scope.formFieldsError = false;
3131
} catch (e) {
32-
// eat $parse error
33-
// console.log('Formly Demo App Error: error parsing data, changes not applied');
3432
$scope.formFieldsError = true;
3533
}
3634
});
@@ -39,8 +37,6 @@ app.controller('home', function($scope, $parse, $rootScope, $window, usingCustom
3937
$scope.formOptions = $parse(newValue)({});
4038
$scope.formOptionsError = false;
4139
} catch (e) {
42-
// eat $parse error
43-
// console.log('Formly Demo App Error: error parsing data, changes not applied');
4440
$scope.formOptionsError = true;
4541
}
4642
});
@@ -52,6 +48,8 @@ app.controller('home', function($scope, $parse, $rootScope, $window, usingCustom
5248
$scope.typeTemplatesButton = 'Use Custom Type Templates';
5349
}
5450

51+
$scope.preConfiguredOptions = formlyOptions.getOptions();
52+
5553
$scope.formFields = [{
5654
key: 'firstName',
5755
type: 'text',
@@ -179,7 +177,8 @@ app.controller('home', function($scope, $parse, $rootScope, $window, usingCustom
179177

180178
$scope.formOptions = {
181179
uniqueFormId: 'formly',
182-
submitCopy: 'Save'
180+
submitCopy: 'Save',
181+
hideSubmit: false
183182
};
184183
$scope.submittedData = null;
185184
$scope.formData = {};

0 commit comments

Comments
 (0)