Skip to content

Commit e54615a

Browse files
committed
Support for default values
Default values in schema will populate the model when form is rendered, but it will not overwrite any existing values.
1 parent f3e023d commit e54615a

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/bootstrap-example.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ <h3>Model</h3>
7070
},
7171
"shoesize": {
7272
"title": "Shoe size",
73-
"type": "number"
73+
"default": 42,
74+
"type": "number",
7475
},
7576
"attributes": {
7677
"type": "object",

src/directives/schema-form.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ angular.module('schemaForm')
88
['$compile','schemaForm',
99
function($compile, schemaForm){
1010

11+
//recurse through the entire schema.
12+
//FIXME: no support for arrays
13+
var traverse = function(schema,fn,path) {
14+
path = path || "";
15+
fn(schema,path);
16+
angular.forEach(schema.properties,function(prop,name){
17+
traverse(prop,fn,path===""?name:path+'.'+name);
18+
});
19+
};
20+
21+
1122

1223
return {
1324
scope: {
@@ -45,6 +56,7 @@ function($compile, schemaForm){
4556
var lastDigest = {};
4657

4758
scope.$watch(function(){
59+
4860
var schema = scope.schema;
4961
var form = scope.initialForm || ['*'];
5062

@@ -79,7 +91,18 @@ function($compile, schemaForm){
7991
element[0].appendChild(frag);
8092

8193
//compile only children
94+
8295
$compile(element.children())(scope);
96+
97+
98+
//ok, now that that is done let's set any defaults
99+
traverse(schema,function(prop,path){
100+
//This is probably not so fast, but a simple solution.
101+
if (angular.isDefined(prop['default'])) {
102+
scope.$eval('model.'+path+' = model.'+path+' || defaltValue',{ defaltValue: prop['default']});
103+
}
104+
});
105+
83106
}
84107
});
85108
}

test/schema-form-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,45 @@ describe('Schema form',function(){
402402
});
403403
});
404404

405+
it('should honor defaults in schema',function(){
406+
407+
inject(function($compile,$rootScope){
408+
var scope = $rootScope.$new();
409+
scope.person = {
410+
name: 'Foobar'
411+
};
412+
413+
scope.schema = {
414+
"type": "object",
415+
"properties": {
416+
"name": {
417+
"type": "string",
418+
"default": "Bar"
419+
},
420+
"nick": {
421+
"type": "string",
422+
"default": "Zeb"
423+
},
424+
"alias": {
425+
"type": "string"
426+
},
427+
}
428+
};
429+
430+
scope.form = ["*"];
431+
432+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
433+
434+
$compile(tmpl)(scope);
435+
$rootScope.$apply();
436+
437+
scope.person.name.should.be.equal('Foobar');
438+
scope.person.nick.should.be.equal('Zeb');
439+
expect(scope.person.alias).to.be.undefined;
440+
441+
});
442+
});
443+
405444

406445

407446
});

0 commit comments

Comments
 (0)