Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84958aa

Browse files
Nhan To-DoanProLoser
authored andcommitted
Update README.md (angular-ui#33)
Improve formatting.
1 parent e4ede6d commit 84958aa

File tree

1 file changed

+13
-42
lines changed

1 file changed

+13
-42
lines changed

README.md

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ Scalable architecture in AngularJS
44
[Resources](Resources.md)
55
-------------
66

7-
87
Intro
98
-------------
10-
119
Originally forked from ProLoser/AngularJS-ORM
1210

1311
The project demonstrates ways to **leverage ui-router to the greatest of it's abilities**, how to **keep your controllers down to 1 line of code**, how to **organize your services** in a completely simplified manner, and how to **leverage resolves** like a god. Keeping your application down to a **tiny handful of directives**. Avoid the nightmare of lifecycle, transition, and session/stateful bugs. How to **keep your `$scope` clean and tidy**. It doesn't require using `controller as` and it **doesn't turn everything into directives**. Write your code to be **angular-agnostic**. Use the router to **manage state, sessions and collections** allowing you to avoid the problems addressed with complicated flux architectures. Sharing references means **no more watchers and subscribers** strewn across your app.
1412

1513
**_WIP: Please help clean up this repo!_**
1614

17-
1815
The StyleGuide
1916
-------------
2017

@@ -25,8 +22,8 @@ In today's code, it's sensible keep modules together and small. HTML, JS and CSS
2522
This is a practice that is becoming predominant and actually screws up a lot of the benefits of `controllerAs` syntax. Instead of namespacing and bundling controller logic, the only benefit you gain is the little `.` dot notation in `ng-model`. The name `vm` does not tell you where the logic came from or what it has to do with, and does not allow you to work with multiple bundles of logic at the same time. As such, it should be completely avoided.
2623

2724
#### _[DISPUTED]_ If you can't open-source your directives, they usually shouldn't exist
28-
A lot of people will create what I refer to as 'one-off' directives. These usually should just be sub-states.
29-
If you create directives specific to your business logic, and aren't focused on purely UI visual implementation (regardless of data, application, etc) then you are too tightly coupling your business logic to your view. You are making it more difficult to quickly refactor your view or view structure. You have to track down where business logic is being executed or modified in multiple places. You start keeping track of data state and lifecycle and implementing things like events and streams because your view lifecycle isn't consistent with your data lifecycle.
25+
A lot of people will create what I refer to as 'one-off' directives. These usually should just be sub-states. If you create directives specific to your business logic, and aren't focused on purely UI visual implementation (regardless of data, application, etc) then you are too tightly coupling your business logic to your view. You are making it more difficult to quickly refactor your view or view structure. You have to track down where business logic is being executed or modified in multiple places. You start keeping track of data state and lifecycle and implementing things like events and streams because your view lifecycle isn't consistent with your data lifecycle.
26+
3027
Instead, 0 business logic in views. Rendering logic in views only. Publicly, reusable, agnostic, unopinionated, highly versatile/reusable view logic.
3128

3229
#### Don't do routing redirects inside services/factories
@@ -49,18 +46,11 @@ Apps that don't let you access their content without being logged in don't have
4946
#### Don't use `controllerAs` in routes. Use it in directives only.
5047
_pasted from slack_
5148

52-
Controller instances are not shareable.
53-
Meaning if you put logic into a controller (`this.doSomething()`) although you can reuse the logic elsewhere, you can’t reuse the instance.
54-
`controllerAs` syntax fixes a few issues, but it misleads people into thinking it’s okay to bloat controllers, which it isn’t (except for directives).
55-
Your stateful logic shouldn’t be in the controller, it should be in something stateful that can be shared
56-
so instead put most of your `this.doSomething()` into a factory, preferrably in a `class` or `object instance`.
57-
Then you can share it across multiple controllers, not just the logic, but the **highly stateful data**:
58-
`resolve: { person: function(Person) { return new Person() } }`
49+
Controller instances are not shareable.
50+
51+
Meaning if you put logic into a controller (`this.doSomething()`) although you can reuse the logic elsewhere, you can’t reuse the instance. `controllerAs` syntax fixes a few issues, but it misleads people into thinking it’s okay to bloat controllers, which it isn’t (except for directives). Your stateful logic shouldn’t be in the controller, it should be in something stateful that can be shared so instead put most of your `this.doSomething()` into a factory, preferrably in a `class` or `object instance`. Then you can share it across multiple controllers, not just the logic, but the **highly stateful data**: `resolve: { person: function(Person) { return new Person() } }`
5952

60-
You inject `person` into multiple controllers and they all share the same data, and update in sync.
61-
So instead of doing `this.doSomething()` in your controller, and keeping track of info about how a `Person` works inside a controller (_which is reusable, but **not shareable**_) you should keep it inside your factories, which are even FURTHER abstracted from the view/angular-centric mindset, but are also instantiatable and manageable.
62-
I can control more concretely when a person is created, destroyed, updated, who has access to it, how it gets reused, etc.
63-
Angular no longer handles the lifecycle of the data, i do.
53+
You inject `person` into multiple controllers and they all share the same data, and update in sync. So instead of doing `this.doSomething()` in your controller, and keeping track of info about how a `Person` works inside a controller (_which is reusable, but **not shareable**_) you should keep it inside your factories, which are even FURTHER abstracted from the view/angular-centric mindset, but are also instantiatable and manageable. I can control more concretely when a person is created, destroyed, updated, who has access to it, how it gets reused, etc. Angular no longer handles the lifecycle of the data, i do.
6454

6555
Controllers are generally 2-10 lines of code so my controller does nothing more than putting my business logic onto the view and occasionally wrapping business logic with view logic and / or route logic:
6656
```
@@ -74,34 +64,17 @@ $scope.save = function(){
7464
});
7565
```
7666

77-
Now lets say you STILL wanted to use `controllerAs` yet still keep things organized the way I described.
78-
You CAN, except your bindings look like this: `<input ng-model=“personCtrl.person.name”>`
79-
instead of just `<input ng-model=“person.name”>`.
80-
That is fairly trivial, but there is another big annoyance I have with it.
81-
**Your view is no longer reusable with different controllers**.
67+
Now lets say you STILL wanted to use `controllerAs` yet still keep things organized the way I described. You CAN, except your bindings look like this: `<input ng-model=“personCtrl.person.name”>` instead of just `<input ng-model=“person.name”>`. That is fairly trivial, but there is another big annoyance I have with it. **Your view is no longer reusable with different controllers**.
8268

83-
Lets say i want to use the same view with a create vs edit controller.
84-
My view bindings have to be `<input ng-model=“createCtrl.person.name”>` or `<input ng-model=“updateCtrl.person.name”>`
85-
or lets just say you call the controller `person` or `personCtrl`.
86-
You could have a `personEditCtrl` inside of a `personViewCtrl`
87-
so who gets which namespace?
69+
Lets say i want to use the same view with a create vs edit controller. My view bindings have to be `<input ng-model=“createCtrl.person.name”>` or `<input ng-model=“updateCtrl.person.name”>` or lets just say you call the controller `person` or `personCtrl`. You could have a `personEditCtrl` inside of a `personViewCtrl` so who gets which namespace?
8870

8971
When you start to build big apps and deal with these design questions, I find `controllerAs` is **NOT** the answer.
90-
I like having brittle scope bindings, for instance you may have cringed when i did `$scope.loading = true` because if you put a `ng-click=“loading = false”` inside of an `ng-if` it won’t work.
91-
Except I _want_ to keep my view-state flags shallow, simple, clean, and I don’t want the view to update them.
92-
I like having 2 controllers, both with their own `loading` flags that are not the same variable.
93-
I prefer not having to namespace all my loading flags by my controller name or variable or state name, and yet my views simply don’t care.
94-
To them, doing `ng-show=personLoading` is the same as doing `ng-show=personCtrl.loading` and
95-
my view becomes more brittle and heavily tied to the controller in use.
72+
I like having brittle scope bindings, for instance you may have cringed when i did `$scope.loading = true` because if you put a `ng-click=“loading = false”` inside of an `ng-if` it won’t work. Except I _want_ to keep my view-state flags shallow, simple, clean, and I don’t want the view to update them. I like having 2 controllers, both with their own `loading` flags that are not the same variable. I prefer not having to namespace all my loading flags by my controller name or variable or state name, and yet my views simply don’t care. To them, doing `ng-show=personLoading` is the same as doing `ng-show=personCtrl.loading` and my view becomes more brittle and heavily tied to the controller in use.
9673

9774
**In directives, it’s a completely different ballgame**
98-
Because *directives* are entirely about view logic, and should almost never do business logic. Period.
99-
The old way of doing directives you put all your shit into a linking function.
100-
A directive controller is essentially identical to a linking function except it is **reusable by other directives**, a visual-widget's externally visible api.
101-
If you look at [ui-select](https://github.com/angular-ui/ui-select) i love `controllerAs` because I can give it methods like `uiSelectCtrl.open()`
102-
and that really is what it’s doing. It’s the controls for my `ui-select` widget.
103-
`personCtrl.person.open()` just doesn’t make sense if you read it. The ‘controller’ (guy doing shit to people) isn’t the one with the method, the object itself has methods that work upon itself.
104-
Doing `personCtrl.open()` is just a sign of bad design, because controllers should be skinny.
75+
Because *directives* are entirely about view logic, and should almost never do business logic. Period. The old way of doing directives you put all your shit into a linking function. A directive controller is essentially identical to a linking function except it is **reusable by other directives**, a visual-widget's externally visible api. If you look at [ui-select](https://github.com/angular-ui/ui-select) i love `controllerAs` because I can give it methods like `uiSelectCtrl.open()` and that really is what it’s doing. It’s the controls for my `ui-select` widget.
76+
77+
`personCtrl.person.open()` just doesn’t make sense if you read it. The ‘controller’ (guy doing shit to people) isn’t the one with the method, the object itself has methods that work upon itself. Doing `personCtrl.open()` is just a sign of bad design, because controllers should be skinny.
10578

10679
#### Never use scope inheritence across controllers (ui-views)
10780
This is like using `$rootScope`, it's equivalent to using global variables and relies upon assumptions that variables will exist. It makes controllers (and views) depend on variables that may or may not exist, and makes it difficult for developers to see where these variables came from. If you wish to use a service, resolve or something inside of a route controller or view, you should **always re-inject the dependency and place it on the scope redundantly**. This is single-handedly the key to ensuring that your codebase has a solid contract and that the quality of your code stands up to refactoring. Even if it means placing the same objects onto the same variables in the same place on the scope, do it. Period.
@@ -114,8 +87,6 @@ Example:
11487
`<div ui-grid ui-grid-sortable ui-grid-paginate>`
11588

11689
#### Use the object to manage _data_ state
117-
Instead of putting heavy load on _view_ state flags that describe how things should _look_, use grammar that
118-
describes the verb-like state of the data or action itself. This can then be repurposed in multiple ways in
119-
in the view as to the visual representation, and is not tied to visual information.
90+
Instead of putting heavy load on _view_ state flags that describe how things should _look_, use grammar that describes the verb-like state of the data or action itself. This can then be repurposed in multiple ways in the view as to the visual representation, and is not tied to visual information.
12091

12192
Instead of `$scope.showSpinner` or `$scope.loading` use `task.uploading` or `project.saving` which could be rendered as a spinner, form, panel, whatever. The point is the **state flag is agnostic about the visual implementation**.

0 commit comments

Comments
 (0)