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

Commit b7cbe31

Browse files
committed
Merge branch 'kentcdodds-add-template'
2 parents 1447314 + 3a7572e commit b7cbe31

File tree

10 files changed

+130
-25
lines changed

10 files changed

+130
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Version numbers correspond to `bower.json` version
66

77
Added `hide` property on fields. Allows devs to conditionally show fields. Also adding `hideExpression` property which is an expression that will be evaluated on the result object (using $parse) to make the api simpler to use.
88

9+
Added `template` property on fields. Allows devs to have one-liner templates. Mainly used for directives so a single template can be used with options. Also improved the id of fields that use templateUrls or templates.
10+
911
## Bug Fixes
1012

1113
## Breaking Changes

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ Forms can be customized with the options below.
9393
>`submitCopy` customizes the submit button copy. Defaults to 'Submit'.
9494
9595
### Creating Form Fields
96-
When constructing fields use the options below to customize each field object. You must set at least a `type` or `templateUrl`.
96+
When constructing fields use the options below to customize each field object. You must set at least a `type`, `template`, or `templateUrl`.
9797

9898
##### type (string)
99-
>`type` is the type of field to be rendered. Either type or templateUrl must be set.
99+
>`type` is the type of field to be rendered. Either type, template, or templateUrl must be set.
100100
101101
###### Default
102102
>`null`
@@ -112,9 +112,16 @@ When constructing fields use the options below to customize each field object. Y
112112
> [`hidden`](#hidden-form-field),
113113
> [`email`](#email-form-field)
114114
115+
---
116+
##### template (string)
117+
>`template` can be set instead of `type` or `templateUrl` to use a custom html template form field. Should be used with one-liners mostly (like a directive). Useful for adding functionality to fields.
118+
119+
###### Default
120+
>`undefined`
121+
115122
---
116123
##### templateUrl (string)
117-
>`templateUrl` can be set instead of `type` to use a custom html template form field. Set a path relative to the root of the application. ie `directives/custom-field.html`
124+
>`templateUrl` can be set instead of `type` or `template` to use a custom html template form field. Set a path relative to the root of the application. ie `directives/custom-field.html`
118125
119126
###### Default
120127
>`undefined`

dist/formly.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,24 @@ angular.module('formly.render').directive('formlyField', [
5454
value: '=formValue'
5555
},
5656
link: function fieldLink($scope, $element, $attr) {
57-
var templateUrl = $scope.options.templateUrl || getTemplateUrl($scope.options.type);
58-
if (templateUrl) {
59-
$http.get(templateUrl, { cache: $templateCache }).success(function (data) {
60-
//template data returned
61-
$element.html(data);
62-
$compile($element.contents())($scope);
63-
});
57+
var template = $scope.options.template;
58+
if (template) {
59+
setElementTemplate(template);
6460
} else {
65-
console.log('Formly Error: template type \'' + $scope.options.type + '\' not supported.');
61+
var templateUrl = $scope.options.templateUrl || getTemplateUrl($scope.options.type);
62+
if (templateUrl) {
63+
$http.get(templateUrl, { cache: $templateCache }).then(function (response) {
64+
setElementTemplate(response.data);
65+
}, function (error) {
66+
console.log('Formly Error: Problem loading template for ' + templateUrl, error);
67+
});
68+
} else {
69+
console.log('Formly Error: template type \'' + $scope.options.type + '\' not supported.');
70+
}
71+
}
72+
function setElementTemplate(templateData) {
73+
$element.html(templateData);
74+
$compile($element.contents())($scope);
6675
}
6776
},
6877
controller: [
@@ -72,8 +81,14 @@ angular.module('formly.render').directive('formlyField', [
7281
if (typeof $scope.options.default !== 'undefined') {
7382
$scope.value = $scope.options.default;
7483
}
84+
var type = $scope.options.type;
85+
if (!type && $scope.options.template) {
86+
type = 'template';
87+
} else if (!type && $scope.options.templateUrl) {
88+
type = 'templateUrl';
89+
}
7590
// set field id to link labels and fields
76-
$scope.id = $scope.formId + $scope.options.type + $scope.index;
91+
$scope.id = $scope.formId + type + $scope.index;
7792
}
7893
]
7994
};

dist/formly.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/formly.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)