Skip to content

Commit 86a7b5d

Browse files
committed
Merge branch 'array-defaults' of https://github.com/Sandreu/angular-schema-form into Sandreu-array-defaults
Conflicts: src/directives/array.js Solved conflict and modfied code to follow code standard (jscs complained)
2 parents 271c896 + 43d03ba commit 86a7b5d

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/directives/array.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,20 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
8484
var len = list.length;
8585
var copy = scope.copyWithIndex(len);
8686
schemaForm.traverseForm(copy, function(part) {
87-
if (part.key && part.schema && angular.isDefined(part.schema['default'])) {
88-
sfSelect(part.key, scope.model, part.schema['default']);
87+
88+
if (part.key) {
89+
var def;
90+
if (angular.isDefined(part['default'])) {
91+
def = part['default'];
92+
}
93+
if (angular.isDefined(part.schema) &&
94+
angular.isDefined(part.schema['default'])) {
95+
def = part.schema['default'];
96+
}
97+
98+
if (angular.isDefined(def)) {
99+
sfSelect(part.key, scope.model, def);
100+
}
89101
}
90102
});
91103

test/directives/schema-form-test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,56 @@ describe('directive',function(){
707707
});
708708
});
709709

710+
it('should handle schema form default in deep structure with array',function(){
711+
712+
inject(function($compile,$rootScope){
713+
var scope = $rootScope.$new();
714+
scope.person = {
715+
"arr":[]
716+
};
717+
718+
scope.schema = {
719+
"type": "object",
720+
"properties": {
721+
"arr" : {
722+
"type": "array",
723+
"items": {
724+
"type": "object",
725+
"title": "Person",
726+
"properties": {
727+
"name": {
728+
"type": "string",
729+
"default": "Name"
730+
},
731+
"nick": {
732+
"type": "string",
733+
"default": "Nick"
734+
},
735+
"alias": {
736+
"type": "string"
737+
}
738+
}
739+
}
740+
}
741+
}
742+
};
743+
744+
//The form defines a fieldset for person, and changes the order of fields
745+
//but titles should come from the schema
746+
scope.form = ['*'];
747+
748+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
749+
750+
$compile(tmpl)(scope);
751+
$rootScope.$apply();
752+
753+
scope.person.arr[0].name.should.be.equal('Name');
754+
scope.person.arr[0].nick.should.be.equal('Nick');
755+
expect(scope.person.arr[0].alias).to.be.undefined;
756+
757+
});
758+
});
759+
710760
it('should skip title if form says "notitle"',function(){
711761

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

0 commit comments

Comments
 (0)