Skip to content

Commit 9b96800

Browse files
committed
More docs on extending: builders!
1 parent be8445f commit 9b96800

File tree

1 file changed

+124
-2
lines changed

1 file changed

+124
-2
lines changed

docs/extending.md

+124-2
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,131 @@ So [make a bower package](http://bower.io/docs/creating-packages/), add the keyw
190190

191191
The builders
192192
------------
193-
TODO: API docs for each builder
193+
A collection of useful builders that cover most cases are in the `sfBuilder` service and is accessable
194+
both from the provider and the service on the property `builders`. There is also a list of "standard"
195+
builders, when in doubt use those.
196+
197+
```js
198+
angular.module('myMod').config(function(sfBuildersProvider) {
199+
200+
// Standard builders
201+
sfBuildersProvider.stdBuilders;
202+
203+
// All builders
204+
sfBuildersProvider.builder.sfField;
205+
sfBuildersProvider.builder.condition;
206+
sfBuildersProvider.builder.ngModel;
207+
sfBuildersProvider.builder.ngModelOptions;
208+
sfBuildersProvider.builder.simpleTransclusion;
209+
sfBuildersProvider.builder.transclusion;
210+
sfBuildersProvider.builder.array;
211+
212+
});
213+
```
214+
215+
Currently the standard builders are:
216+
```js
217+
var stdBuilders = [
218+
builders.sfField,
219+
builders.ngModel,
220+
builders.ngModelOptions,
221+
builders.condition
222+
];
223+
```
224+
225+
226+
### builders.sfField
227+
The `sfField` builder adds the `sf-field="..."` directive to *the first child element* in the template,
228+
giving it a correct value. The value is an id number that identifies that specific form object.
229+
230+
The `sf-field` directive exports the form definition object as `form` on scope and as a lot of useful functions.
231+
232+
As a rule of thumb you always want this builder.
233+
234+
### builders.condition
235+
The `condition` builder checks the form definition for the option `condition`. If it's present it adds a
236+
`ng-if` to all top level elements in the template.
237+
238+
You usually want this as well.
239+
240+
### builder.ngModel
241+
The `ngModel` builder is maybe the most important builder. It makes sure you get a proper binding to
242+
your model value.
243+
244+
The `ngModel` builder queries the DOM of the template for all elements that have the attribute `sf-field-model`. Your template may have several of them. `sf-field-model` is *not* a directive,
245+
but depending on it's value the `ngModel` builder will take three different actions.
246+
247+
248+
#### sf-field-model
249+
Just `sf-field-model` or `sf-field-model=""` tells the builder to add a `ng-model` directive to this element.
250+
This is a common use case.
251+
252+
Ex:
253+
DOM before `ngModel` builder:
254+
```html
255+
<div>
256+
<input sf-field-model type="text">
257+
</div>
258+
```
259+
DOM after `ngModel` builder:
260+
```html
261+
<div>
262+
<input sf-field-model ng-model="model['name']" type="text">
263+
</div>
264+
```
265+
266+
#### sf-field-model="<attribute name>"
267+
Given a value the `ngModel` builder will treat that value as a *attribute name* and instead of slapping
268+
on a `ng-model` set the specified attributes value. It sets it to the same value as the `ng-model` would have gotten.
269+
270+
Ex:
271+
DOM before `ngModel` builder:
272+
```html
273+
<div sf-field-model="my-directive">
274+
<input sf-field-model type="text">
275+
</div>
276+
```
277+
DOM after `ngModel` builder:
278+
```html
279+
<div my-directive="model['name']">
280+
<input sf-field-model ng-model="model['name']" type="text">
281+
</div>
282+
```
283+
284+
#### sf-field-model="replaceAll"
285+
With the special value *replaceAll* the `ngModel` builder will instead loop over every attribute on the
286+
element and do a string replacement of `"$$value$$"` with the proper model value.
287+
288+
Ex:
289+
DOM before `ngModel` builder:
290+
```html
291+
<div>
292+
<input sf-field-model="replaceAll"
293+
ng-model="$$value$$"
294+
ng-class="{'large': $$value$$.length > 10}"
295+
type="text">
296+
</div>
297+
```
298+
DOM after `ngModel` builder:
299+
```html
300+
<div>
301+
<input sf-field-model="replaceAll"
302+
ng-model="model['name']"
303+
ng-class="{'large': model[name].length > 10}"
304+
type="text">
305+
</div>
306+
```
307+
308+
### builders.ngModelOptions
309+
If the form definition has a `ngModelOptions` option specified this builder will slap on a `ng-model-options`
310+
attribute to *the first child element* in the template.
311+
312+
313+
### builder.simpleTransclusion
314+
The `simpleTransclusion` builder will recurse and build form items, useful for fieldsets etc. This builder
315+
is simple because it only appends children to the first child element and only checks `form.items`.
194316

195317

196318
Useful directives
197319
-----------------
198-
TODO: more in depth about schema-validate and sf-messages
320+
TODO: more in depth about schema-validate, sf-messages and sf-field

0 commit comments

Comments
 (0)