-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathapp.js
55 lines (46 loc) · 1.57 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { getPostsTojs, getIsFetching } from '../selectors'
export default function app() {
return {
restrict: 'E',
controllerAs: 'app',
controller: AppController,
template: require('./app.html'),
scope: {}
};
}
class AppController {
constructor($ngRedux, AsyncActions, $scope) {
const unsubscribe = $ngRedux.connect(this.mapStateToThis, AsyncActions)((selectedState, actions) => {
this.componentWillReceiveStateAndActions(selectedState, actions);
Object.assign(this, selectedState, actions);
});
this.options = ['angularjs', 'frontend'];
this.handleChange = this.handleChange.bind(this);
this.handleRefreshClick = this.handleRefreshClick.bind(this);
this.fetchPostsIfNeeded(this.selectedReddit);
$scope.$on('$destroy', unsubscribe);
}
componentWillReceiveStateAndActions(nextState, nextActions) {
if (nextState.selectedReddit !== this.selectedReddit) {
nextActions.fetchPostsIfNeeded(nextState.selectedReddit);
}
}
handleChange(nextReddit) {
this.selectReddit(nextReddit);
}
handleRefreshClick() {
this.invalidateReddit(this.selectedReddit);
this.fetchPostsIfNeeded(this.selectedReddit);
}
mapStateToThis(state) {
const { selectedReddit, postsByReddit } = state;
return {
selectedReddit,
// Use selectors here
posts: getPostsTojs(state),
isFetching: getIsFetching(state),
// Or get value from the state directly without selectors
lastUpdated: postsByReddit.get(selectedReddit) && postsByReddit.get(selectedReddit).get('lastUpdated')
};
}
}