Skip to content

Commit 6e3279d

Browse files
committed
Defined array defaults in schema used for new array element
1 parent ac29fb9 commit 6e3279d

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/directives/array.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
7676
var len = list.length;
7777
var copy = scope.copyWithIndex(len);
7878
schemaForm.traverseForm(copy, function(part) {
79-
if (part.key && angular.isDefined(part['default'])) {
80-
sfSelect(part.key, scope.model, part['default']);
79+
var def;
80+
if (angular.isDefined(part['default'])) def = part['default'];
81+
if (angular.isDefined(part['schema']) && angular.isDefined(part['schema']['default'])) def = part['schema']['default'];
82+
83+
if (part.key && angular.isDefined(def)) {
84+
sfSelect(part.key, scope.model, def);
8185
}
8286
});
8387

test/directives/schema-form-test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,56 @@ describe('directive',function(){
651651
});
652652
});
653653

654+
it('should handle schema form default in deep structure with array',function(){
655+
656+
inject(function($compile,$rootScope){
657+
var scope = $rootScope.$new();
658+
scope.person = {
659+
"arr":[]
660+
};
661+
662+
scope.schema = {
663+
"type": "object",
664+
"properties": {
665+
"arr" : {
666+
"type": "array",
667+
"items": {
668+
"type": "object",
669+
"title": "Person",
670+
"properties": {
671+
"name": {
672+
"type": "string",
673+
"default": "Name"
674+
},
675+
"nick": {
676+
"type": "string",
677+
"default": "Nick"
678+
},
679+
"alias": {
680+
"type": "string"
681+
}
682+
}
683+
}
684+
}
685+
}
686+
};
687+
688+
//The form defines a fieldset for person, and changes the order of fields
689+
//but titles should come from the schema
690+
scope.form = ['*'];
691+
692+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
693+
694+
$compile(tmpl)(scope);
695+
$rootScope.$apply();
696+
697+
scope.person.arr[0].name.should.be.equal('Name');
698+
scope.person.arr[0].nick.should.be.equal('Nick');
699+
expect(scope.person.arr[0].alias).to.be.undefined;
700+
701+
});
702+
});
703+
654704
it('should skip title if form says "notitle"',function(){
655705

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

0 commit comments

Comments
 (0)