From 35bbcfdf79b318cbd9ca15e9b0e85de3c20c5636 Mon Sep 17 00:00:00 2001 From: Satoshi Tanimoto Date: Wed, 2 Mar 2016 22:35:16 -0800 Subject: [PATCH] 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. --- src/directives/schema-form.js | 2 +- test/directives/schema-form-test.js | 53 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/directives/schema-form.js b/src/directives/schema-form.js index 53d473939..1b21885e4 100644 --- a/src/directives/schema-form.js +++ b/src/directives/schema-form.js @@ -170,7 +170,7 @@ angular.module('schemaForm') // part of the form or schema is chnaged without it being a new instance. scope.$on('schemaFormRedraw', function() { var schema = scope.schema; - var form = scope.initialForm || ['*']; + var form = scope.initialForm ? angular.copy(scope.initialForm) : ['*']; if (schema) { render(schema, form); } diff --git a/test/directives/schema-form-test.js b/test/directives/schema-form-test.js index 2d21e5120..a34553c86 100644 --- a/test/directives/schema-form-test.js +++ b/test/directives/schema-form-test.js @@ -1768,6 +1768,59 @@ describe('directive',function(){ }); }); + it('should redraw form with proper defaults on schemaFormRedraw event',function(done) { + + inject(function($compile, $rootScope){ + var scope = $rootScope.$new(); + scope.person = {}; + + scope.schema = { + type: 'object', + properties: { + name: {type: 'string'} + } + }; + + scope.form = [{ + key: 'name', + type: 'text' + }]; + + scope.options = {formDefaults: {}}; + + var tmpl = angular.element('
'); + + $compile(tmpl)(scope); + $rootScope.$apply(); + + expect(tmpl.find('input').attr('disabled')).to.be.undefined; + + var disable, enable; + disable = function () { + // form element should be disabled + scope.options.formDefaults.readonly = true; + scope.$broadcast('schemaFormRedraw'); + $rootScope.$apply(); + expect(tmpl.find('input').attr('disabled')).eq('disabled'); + + // try to re-enable it by modifying global option + setTimeout(enable, 0); + }; + + enable = function () { + // form element should be back to enabled + scope.options.formDefaults.readonly = false; + scope.$broadcast('schemaFormRedraw'); + $rootScope.$apply(); + expect(tmpl.find('input').attr('disabled')).to.be.undefined; + + done(); + } + + setTimeout(disable, 0); + }); + }); + it('should use supplied template with template field type',function() { inject(function($compile, $rootScope){