Skip to content

Commit eb54c13

Browse files
author
Satoshi Tanimoto
committed
Fix for json-schema-form#121 to redraw with proper defaults
The problem was that initialForm was used to generate a form on redraw event, but when it was an object type, it was modified in-place by rendering function. So, when the next redraw event happened, initialForm already had the properties defined, and it prevented the default values from being populated into them.
1 parent 7f29822 commit eb54c13

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

dist/schema-form.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2677,7 +2677,7 @@ angular.module('schemaForm')
26772677
// part of the form or schema is chnaged without it being a new instance.
26782678
scope.$on('schemaFormRedraw', function() {
26792679
var schema = scope.schema;
2680-
var form = scope.initialForm || ['*'];
2680+
var form = scope.initialForm ? angular.copy(scope.initialForm) : ['*'];
26812681
if (schema) {
26822682
render(schema, form);
26832683
}

dist/schema-form.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directives/schema-form.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ angular.module('schemaForm')
170170
// part of the form or schema is chnaged without it being a new instance.
171171
scope.$on('schemaFormRedraw', function() {
172172
var schema = scope.schema;
173-
var form = scope.initialForm || ['*'];
173+
var form = scope.initialForm ? angular.copy(scope.initialForm) : ['*'];
174174
if (schema) {
175175
render(schema, form);
176176
}

test/directives/schema-form-test.js

+53
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,59 @@ describe('directive',function(){
18221822
});
18231823
});
18241824

1825+
it('should redraw form with proper defaults on schemaFormRedraw event',function(done) {
1826+
1827+
inject(function($compile, $rootScope){
1828+
var scope = $rootScope.$new();
1829+
scope.person = {};
1830+
1831+
scope.schema = {
1832+
type: 'object',
1833+
properties: {
1834+
name: {type: 'string'}
1835+
}
1836+
};
1837+
1838+
scope.form = [{
1839+
key: 'name',
1840+
type: 'text'
1841+
}];
1842+
1843+
scope.options = {formDefaults: {}};
1844+
1845+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person" sf-options="options"></form>');
1846+
1847+
$compile(tmpl)(scope);
1848+
$rootScope.$apply();
1849+
1850+
expect(tmpl.find('input').attr('disabled')).to.be.undefined;
1851+
1852+
var disable, enable;
1853+
disable = function () {
1854+
// form element should be disabled
1855+
scope.options.formDefaults.readonly = true;
1856+
scope.$broadcast('schemaFormRedraw');
1857+
$rootScope.$apply();
1858+
expect(tmpl.find('input').attr('disabled')).eq('disabled');
1859+
1860+
// try to re-enable it by modifying global option
1861+
setTimeout(enable, 0);
1862+
};
1863+
1864+
enable = function () {
1865+
// form element should be back to enabled
1866+
scope.options.formDefaults.readonly = false;
1867+
scope.$broadcast('schemaFormRedraw');
1868+
$rootScope.$apply();
1869+
expect(tmpl.find('input').attr('disabled')).to.be.undefined;
1870+
1871+
done();
1872+
}
1873+
1874+
setTimeout(disable, 0);
1875+
});
1876+
});
1877+
18251878
it('should use supplied template with template field type',function() {
18261879

18271880
inject(function($compile, $rootScope){

0 commit comments

Comments
 (0)