Skip to content

Commit 459a1a5

Browse files
committed
Schema now merges properly with form in fieldsets
1 parent f12e9d3 commit 459a1a5

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

karma.conf.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ module.exports = function(config) {
1515
// list of files / patterns to load in the browser
1616
files: [
1717
'http://code.jquery.com/jquery-2.1.0.min.js',
18-
'https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js',
19-
'http://code.angularjs.org/1.2.14/angular-mocks.js',
18+
'https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js',
19+
'https://code.angularjs.org/1.2.16/angular-mocks.js',
2020
'bower_components/tv4/tv4.js',
2121
'src/module.js',
2222
'src/services/*.js',
@@ -81,7 +81,7 @@ module.exports = function(config) {
8181
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
8282
// - PhantomJS
8383
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
84-
browsers: ['PhantomJS'],
84+
browsers: [],
8585

8686

8787
// If browser does not capture in given timeout [ms], kill it
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<fieldset ng-disabled="form.readonly">
22
<legend ng-show="form.title">{{ form.title }}</legend>
3-
<bootstrap-decorator ng-repeat="item in form.items" form="item"></bootstrap-decorator>
3+
<sf-decorator ng-repeat="item in form.items" form="item"></sf-decorator>
44
</fieldset>

src/services/schema-form.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
angular.module('schemaForm').factory('schemaForm',[function(){
77
var service = {};
8-
8+
99
service.merge = function(schema,form,ignore) {
1010
form = form || ["*"];
1111

@@ -24,12 +24,18 @@ angular.module('schemaForm').factory('schemaForm',[function(){
2424
//We look at the supplied form and extend it with schema standards
2525
var lookup = stdForm.lookup;
2626
return form.map(function(obj){
27-
27+
2828
//handle the shortcut with just a name
2929
if (typeof obj === 'string') {
3030
obj = { key: obj };
3131
}
3232

33+
//if it's a type with items, merge 'em!
34+
if (obj.items) {
35+
obj.items = service.merge(schema,obj.items,ignore);
36+
}
37+
38+
3339
//extend with std form from schema.
3440
if (obj.key && lookup[obj.key]) {
3541
return angular.extend(lookup[obj.key],obj);
@@ -133,6 +139,7 @@ angular.module('schemaForm').factory('schemaForm',[function(){
133139
var f = stdFormObj(schema,options);
134140
f.type = 'fieldset';
135141
f.items = [];
142+
options.lookup[options.path] = f;
136143

137144
//recurse down into properties
138145
angular.forEach(schema.properties,function(v,k){

test/schema-form-test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,65 @@ describe('Schema form',function(){
468468
});
469469
});
470470

471+
it('should handle schema form defaults in deep structure',function(){
472+
473+
inject(function($compile,$rootScope){
474+
var scope = $rootScope.$new();
475+
scope.person = {
476+
name: 'Foobar'
477+
};
478+
479+
scope.schema = {
480+
"type": "object",
481+
"properties": {
482+
"props" : {
483+
"type": "object",
484+
"title": "Person",
485+
"properties": {
486+
"name": {
487+
"type": "string",
488+
"title": "Name"
489+
},
490+
"nick": {
491+
"type": "string",
492+
"title": "Nick"
493+
},
494+
"alias": {
495+
"type": "string",
496+
"title": "Alias"
497+
}
498+
}
499+
}
500+
}
501+
};
502+
503+
//The form defines a fieldset for person, and changes the order of fields
504+
//but titles should come from the schema
505+
scope.form = [{
506+
type: 'fieldset',
507+
key: 'props',
508+
items: [
509+
'props.nick',
510+
'props.name',
511+
'props.alias'
512+
]
513+
}];
514+
515+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
516+
517+
$compile(tmpl)(scope);
518+
$rootScope.$apply();
519+
520+
tmpl.children().length.should.be.eq(1);
521+
var labels = tmpl.children().children().find('label');
522+
labels.eq(0).text().should.equal('Nick');
523+
labels.eq(1).text().should.equal('Name');
524+
labels.eq(2).text().should.equal('Alias');
525+
526+
});
527+
});
528+
529+
471530
it('should skip title if form says "notitle"',function(){
472531

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

0 commit comments

Comments
 (0)