| 1 |
<!DOCTYPE html> |
| 2 |
<html ng-app="builderExample"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>Builder example</title> |
| 6 |
</head> |
| 7 |
<body> |
| 8 |
|
| 9 |
<div custom-build></div> |
| 10 |
|
| 11 |
<script src="../bower_components/angular/angular.js" charset="utf-8"></script> |
| 12 |
<script src="../dist/schema-form.js" charset="utf-8"></script> |
| 13 |
<script src="../bower_components/objectpath/lib/ObjectPath.js"></script> |
| 14 |
|
| 15 |
<script type="text/javascript"> |
| 16 |
angular.module('builderExample', ['schemaForm']) |
| 17 |
.run(function($templateCache) { |
| 18 |
// A template to use |
| 19 |
$templateCache.put('example-template.html', '<h1>Hello {{world}}</h1>'); |
| 20 |
}) |
| 21 |
.directive('customBuild', function($compile, sfBuilder) { |
| 22 |
return { |
| 23 |
link: function(scope, element, attrs) { |
| 24 |
scope.world = 'World'; |
| 25 |
|
| 26 |
// First a "canonical form definition", usually this is created by merging form |
| 27 |
// definition from user and a schema. |
| 28 |
var formDef = [ |
| 29 |
{type: 'example'} |
| 30 |
]; |
| 31 |
|
| 32 |
// A decorator is just an object that matches form `type` to a metadata object |
| 33 |
// `replace` property is a boolean, if false the "old" way to build forms is |
| 34 |
// used for backwards compatability. |
| 35 |
// `builder` is a function or a list of helper functions to manipulate the DOM during the build. |
| 36 |
// For example to set proper ng-model value, or include child items |
| 37 |
var decorator = { |
| 38 |
example: {template: 'example-template.html', replace: true, builder: []} |
| 39 |
}; |
| 40 |
|
| 41 |
// The builder creates a nice document fragment for you to place in the form. |
| 42 |
var documentFragment = sfBuilder.build(formDef, decorator); |
| 43 |
|
| 44 |
// Then plop the fragment into the element |
| 45 |
element[0].appendChild(documentFragment); |
| 46 |
|
| 47 |
// It isn't compiled yet though, so let's do that. |
| 48 |
$compile(element.children())(scope); |
| 49 |
} |
| 50 |
}; |
| 51 |
}); |
| 52 |
|
| 53 |
</script> |
| 54 |
</body> |
| 55 |
</html> |