Skip to content

Commit 35bbcfd

Browse files
author
Satoshi Tanimoto
committed
Fix for #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 e35cdde commit 35bbcfd

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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
@@ -1768,6 +1768,59 @@ describe('directive',function(){
17681768
});
17691769
});
17701770

1771+
it('should redraw form with proper defaults on schemaFormRedraw event',function(done) {
1772+
1773+
inject(function($compile, $rootScope){
1774+
var scope = $rootScope.$new();
1775+
scope.person = {};
1776+
1777+
scope.schema = {
1778+
type: 'object',
1779+
properties: {
1780+
name: {type: 'string'}
1781+
}
1782+
};
1783+
1784+
scope.form = [{
1785+
key: 'name',
1786+
type: 'text'
1787+
}];
1788+
1789+
scope.options = {formDefaults: {}};
1790+
1791+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person" sf-options="options"></form>');
1792+
1793+
$compile(tmpl)(scope);
1794+
$rootScope.$apply();
1795+
1796+
expect(tmpl.find('input').attr('disabled')).to.be.undefined;
1797+
1798+
var disable, enable;
1799+
disable = function () {
1800+
// form element should be disabled
1801+
scope.options.formDefaults.readonly = true;
1802+
scope.$broadcast('schemaFormRedraw');
1803+
$rootScope.$apply();
1804+
expect(tmpl.find('input').attr('disabled')).eq('disabled');
1805+
1806+
// try to re-enable it by modifying global option
1807+
setTimeout(enable, 0);
1808+
};
1809+
1810+
enable = function () {
1811+
// form element should be back to enabled
1812+
scope.options.formDefaults.readonly = false;
1813+
scope.$broadcast('schemaFormRedraw');
1814+
$rootScope.$apply();
1815+
expect(tmpl.find('input').attr('disabled')).to.be.undefined;
1816+
1817+
done();
1818+
}
1819+
1820+
setTimeout(disable, 0);
1821+
});
1822+
});
1823+
17711824
it('should use supplied template with template field type',function() {
17721825

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

0 commit comments

Comments
 (0)