Skip to content

Commit b390a7e

Browse files
committed
added constants separation. closes johnpapa#191
1 parent c777c21 commit b390a7e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,31 @@ Unit testing helps maintain clean code, as such I included some of my recommenda
25382538
.constant('moment', moment);
25392539
})();
25402540
```
2541+
2542+
###### [Style [Y241](#style-y241)]
2543+
2544+
- Use constants for values that do not change and do not come from another service. When constants are used only for a module that may be reused in multiple applications, place constants in a file per module named after the module. Until this is required, keep constants in the main module in a `constants.js` file.
2545+
2546+
*Why?*: A value that may change, even infrequently, should be retrieved from a service so you do not have to change the source code. For example, a url for a data service could be placed in a constants but a better place would be to load it from a web service.
2547+
2548+
*Why?*: Constants can be injected into any angular component, including providers.
2549+
2550+
*Why?*: When an application is separated into modules that may be reused in other applications, each standlone module should be able to operation on its own including any dependent constants.
2551+
2552+
```javascript
2553+
// Constants used by the entire app
2554+
angular
2555+
.module('app.core')
2556+
.constant('moment', moment);
2557+
2558+
// Constants used only by the sales module
2559+
angular
2560+
.module('app.sales')
2561+
.constant('events', {
2562+
ORDER_CREATED: 'event_order_created',
2563+
INVENTORY_DEPLETED: 'event_inventory_depleted'
2564+
});
2565+
```
25412566
25422567
**[Back to top](#table-of-contents)**
25432568

0 commit comments

Comments
 (0)