Skip to content
This repository was archived by the owner on Jun 14, 2018. It is now read-only.

Commit

Permalink
added directive guidelines 007 & 008
Browse files Browse the repository at this point in the history
closes #93
merges #94
  • Loading branch information
andrewconnell committed Jan 16, 2016
1 parent 4a836e7 commit a9e427a
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion docs/guides/DIRECTIVE-CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Most of the guidance is based on the [Angular Material](http://material.angularj
- (004) [Module Naming](#module-naming)
- (005) [Use External Modules](#use-external-modules)
- (006) [Directive Attributes](#directive-attributes)
- (007) [Directive Attribute Validation](#directive-attribute-validation)
- (008) [Error Messages](#error-messages)
- [Testing](#testing)
- (101) [Verify Coverage with Tests](#verify-coverage-with-tests)
- (102) [Organizing Tests](#organizing-tests)
Expand Down Expand Up @@ -110,7 +112,7 @@ interface IButtonScope extends ng.IScope {

class ButtonDirective implements ng.IDirective {
public restrict: string = 'E';

public static factory(): ng.IDirectiveFactory {
const directive: ng.IDirectiveFactory = () => new ButtonDirective();

Expand All @@ -133,6 +135,57 @@ Any element directive that requires custom attributes must prefix those attribut

However if you can reuse an existing HTML attribute that would be used for the same or similar purpose, do that. For example, don't create a `uif-value` attribute on a input control to set the value of the control when you can easily use `value` which would do the same thing on a `<input type="text" />` control.

###Directive Attribute Validation
###### [Guideline [007](#guideline-007)]

Directives that have an attribute that a developer can set, if the attribute has expected & defined values it should:

- include an enumeration that includes all possible options
- validation logic that logs an error on failures in the console using the Angular `$log.error()`

For example, here's what the enumeration looks like for the `uif-icon` directive:

```javascript
export enum IconEnum {
alert,
alert2
}
```

Here is an example of validation within a directive's `link()` function:

```javascript
public link(scope: IIconScope,
instanceElement: ng.IAugmentedJQuery,
attrs: ng.IAttributes,
controller: IconController): void {
// add watcher
scope.$watch('uifType', (newValule: string, oldValue: string) => {
// verify a valid icon was passed in
if (IconEnum[newValule] === undefined) {
controller.$log.error('Error [ngOfficeUiFabric] {{module}} - {{error}}: {{error details + link to enum of options}}');
}
});
};
```

> Look at the `uif-icon` directive (in release v0.2.0+) for an example of how this is done.
###Error Messages
###### [Guideline [008](#guideline-008)]

When logging errors from the library to the console, always use the Angular `$log` service.

Error messages should have the format of:

```
Error [ngOfficeUiFabric] {{module}} - {{error}}: {{error details + link to enum of options}}
```

- **{{module}}**: should be the name of the module the error is logged from
- **{{error}}**: short name of the error
- **{{error details}}**: detailed description of the error; if you can reference something, do so... for instance, if something failed validation you could point to the enumeration file within the `master` branch of the repo.

**[Back to top](#table-of-contents)**


Expand Down

0 comments on commit a9e427a

Please sign in to comment.