Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 64853b4

Browse files
committed
Update TIPS-AND-TRICKS.md
1 parent b6fe020 commit 64853b4

File tree

1 file changed

+51
-42
lines changed

1 file changed

+51
-42
lines changed

TIPS-AND-TRICKS.md

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,55 @@ Apps that don't let you access their content without being logged in don't have
2727
#### Don't use `controllerAs` in routes. Use it in directives only.
2828
_pasted from slack_
2929

30-
controller instances are not shareable. Meaning if you put logic into a controller (`this.doSomething()`) although you can reuse the logic elsewhere, you can’t reuse the instance
31-
controllerAs syntax fixes a few issues, but I believe it will mislead people into thinking it’s okay to bloat controllers, which it isn’t (except for directives)
32-
the reason is because your special logic that’s usually stateful shouldn’t be in the controller, it should be in something stateful that can be shared
33-
so you should instead put most of your `this.doSomething()` into a factory, preferrably (if you follow my talks) a class or object instance
34-
then you can share this factory/class/instance across multiple controllers, and not just the logic is shared, but also the highly stateful data
30+
Controller instances are not shareable.
31+
Meaning if you put logic into a controller (`this.doSomething()`) although you can reuse the logic elsewhere, you can’t reuse the instance.
32+
`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).
33+
Your stateful logic shouldn’t be in the controller, it should be in something stateful that can be shared
34+
so instead put most of your `this.doSomething()` into a factory, preferrably in a `class` or `object instance`.
35+
Then you can share it across multiple controllers, not just the logic, but the **highly stateful data**:
3536
`resolve: { person: function(Person) { return new Person() } }`
36-
you inject `person` into multiple controllers and they all share the same data, and they all update in sync
37-
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
38-
I can control more concretely when a person is created, destroyed, updated, who has access to it, how it gets reused, etc
39-
angular no longer handles the lifecycle of the data, i do
40-
my route-controllers are generally 2-10 lines of code
41-
$scope.person = person
42-
or `$scope.save = function(){ $scope.loading = true; person.save().then({ $scope.loading = false }) }` (edited)
43-
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 (edited)
44-
now lets say you STILL wanted to use controllerAs yet still keep things organized the way i described
45-
you CAN, except your bindings look like this: `<input ng-model=“personCtrl.person.name”>`
46-
instead of just `<input ng-model=“person.name”>`
47-
that is fairly trivial, mind you, but there is another big annoyance I have with it
48-
your view is no longer reusable with different controllers
49-
lets say i want to use the same view with a create vs edit controller
50-
my view bindings have to be `<input ng-model=“createCtrl.person.name”>` or `<input ng-model=“updateCtrl.person.name”>`
51-
or lets just say you call the controller `person` or `personCtrl`
52-
then you could have a view person controller and inside of it a edit person controller
53-
who gets which name?
54-
no, when you start to build big apps and deal with these design questions, I find `controllerAs` is NOT the answer
55-
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
56-
except i want to keep my view-state flags shallow, simple, clean, and I don’t want the view to update them
57-
I like having 2 controllers, both with their own `loading` flags
58-
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
59-
doing `ng-show=personLoading` is the same as doing `ng-show=personCtrl.loading`
60-
my view becomes more brittle and heavily tied to controllers
61-
it’s a weird like way of walking this fine line of things
62-
but i’ve come to find i’m extremely happy with the architecture i’ve built and it scales ridiculously well
63-
now in directives, it’s a completely different ballgame
64-
that’s because *directives* are entirely about view logic, and should almost never do business logic. Period.
65-
and anyone who things otherwise I humbly disagree with.
66-
Now the thing is, the old way of doing directives you put all your shit into a linking function
67-
a directive controller is essentially identical to a linking function except it is reusable by other directives
68-
so it’s a visual-widgets externally visible api
69-
so if you look at `ui-select` i love controllerAs because I can give it methods like `uiSelectCtrl.open()`
70-
and that really is what it’s doing. It’s the controls for my ui-select widget
71-
`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
72-
and doing `personCtrl.open()` is just a sign of bad design, because skinny controllers
37+
38+
You inject `person` into multiple controllers and they all share the same data, and update in sync.
39+
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.
40+
I can control more concretely when a person is created, destroyed, updated, who has access to it, how it gets reused, etc.
41+
Angular no longer handles the lifecycle of the data, i do.
42+
43+
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:
44+
```
45+
$scope.person = person;
46+
$scope.save = function(){
47+
$scope.loading = true;
48+
person.save().then(() => {
49+
$scope.loading = false
50+
});
51+
});
52+
```
53+
54+
Now lets say you STILL wanted to use `controllerAs` yet still keep things organized the way I described.
55+
You CAN, except your bindings look like this: `<input ng-model=“personCtrl.person.name”>`
56+
instead of just `<input ng-model=“person.name”>`.
57+
That is fairly trivial, but there is another big annoyance I have with it.
58+
**Your view is no longer reusable with different controllers**.
59+
60+
Lets say i want to use the same view with a create vs edit controller.
61+
My view bindings have to be `<input ng-model=“createCtrl.person.name”>` or `<input ng-model=“updateCtrl.person.name”>`
62+
or lets just say you call the controller `person` or `personCtrl`.
63+
You could have a `personEditCtrl` inside of a `personViewCtrl`
64+
so who gets which namespace?
65+
66+
When you start to build big apps and deal with these design questions, I find `controllerAs` is **NOT** the answer.
67+
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.
68+
Except I _want_ to keep my view-state flags shallow, simple, clean, and I don’t want the view to update them.
69+
I like having 2 controllers, both with their own `loading` flags that are not the same variable.
70+
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.
71+
To them, doing `ng-show=personLoading` is the same as doing `ng-show=personCtrl.loading` and
72+
my view becomes more brittle and heavily tied to the controller in use.
73+
74+
**In directives, it’s a completely different ballgame**
75+
Because *directives* are entirely about view logic, and should almost never do business logic. Period.
76+
The old way of doing directives you put all your shit into a linking function.
77+
A directive controller is essentially identical to a linking function except it is **reusable by other directives**, a visual-widget's externally visible api.
78+
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()`
79+
and that really is what it’s doing. It’s the controls for my `ui-select` widget.
80+
`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.
81+
Doing `personCtrl.open()` is just a sign of bad design, because controllers should be skinny.

0 commit comments

Comments
 (0)