Skip to content

Commit 2c7b1fe

Browse files
committed
no more collect
1 parent 4faea58 commit 2c7b1fe

File tree

3 files changed

+110
-71
lines changed

3 files changed

+110
-71
lines changed

README.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ new Vue({
2929
// store.js
3030
import { createStore } from 'redux';
3131

32-
cosnt initialState = {
32+
const initialState = {
3333
todos: []
3434
};
3535

@@ -58,13 +58,7 @@ export default AppStore;
5858
import { connect } from 'vua-redux';
5959

6060
const App = {
61-
props: ['some-prop', 'another-prop'],
62-
63-
/**
64-
* everything you do with vue component props
65-
* you can do inside collect key
66-
*/
67-
collect: {
61+
props: {
6862
todos: {
6963
type: Array,
7064
},
@@ -78,7 +72,7 @@ const App = {
7872
const todo = this.$refs.input.value;
7973
this.addTodo(todo);
8074
}
81-
}.
75+
},
8276

8377
render(h) {
8478
return <div>
@@ -94,13 +88,13 @@ const App = {
9488
}
9589
};
9690

97-
function mapStateAsProps(state) {
91+
function mapStateToProps(state) {
9892
return {
9993
todos: state.todos
10094
};
10195
}
10296

103-
function mapActionAsProps(dispatch) {
97+
function mapActionToProps(dispatch) {
10498
return {
10599
addTodo(todo) {
106100
dispatch({
@@ -111,6 +105,6 @@ function mapActionAsProps(dispatch) {
111105
}
112106
}
113107

114-
export default connect(mapStateAsProps, mapActionsAsProps)(App);
108+
export default connect(mapStateToProps, mapActionToProps)(App);
115109

116110
```

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vua-redux",
3-
"version": "0.2.5",
3+
"version": "0.3.0",
44
"description": "vue redux binding higher order component",
55
"author": "Nadim Tuhin",
66
"repository": {
@@ -27,8 +27,5 @@
2727
"babel-preset-stage-0": "^6.5.0",
2828
"expect": "^1.20.2",
2929
"mocha": "^3.0.2"
30-
},
31-
"dependencies": {
32-
"lodash": "^4.16.1"
3330
}
3431
}

src/connect.js

Lines changed: 103 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,112 @@
1-
import noop from 'lodash/noop';
21
import normalizeProps from './normalizeProps';
32

4-
export default function connect(mapStateAsProps = noop, mapActionsAsProps = noop) {
5-
return (children) => {
6-
const props = children.props || {};
7-
const subscriptions = children.collect || {};
3+
function noop() {
4+
}
85

9-
const allProps = {
10-
...normalizeProps(props),
11-
...normalizeProps(subscriptions)
12-
};
6+
function getStore(component) {
7+
return component.$store;
8+
}
9+
10+
function getAttrs(component) {
11+
return component.$parent._vnode.data.attrs;
12+
}
13+
14+
function getStates(component, mapStateToProps) {
15+
const store = getStore(component);
16+
const attrs = getAttrs(component);
17+
18+
return mapStateToProps(store.getState(), attrs) || {};
19+
}
20+
21+
function getActions(component, mapActionsToProps) {
22+
const store = getStore(component);
23+
24+
return mapActionsToProps(store.dispatch, getAttrs.bind(null, component)) || {};
25+
}
26+
27+
function getProps(component) {
28+
let props = {};
29+
const attrs = getAttrs(component);
30+
const stateNames = component.vuaReduxStateNames;
31+
const actionNames = component.vuaReduxActionNames;
32+
33+
for (let ii = 0; ii < stateNames.length; ii++) {
34+
props[stateNames[ii]] = component[stateNames[ii]];
35+
}
1336

14-
children.props = allProps;
15-
delete children.collect;
37+
for (let ii = 0; ii < actionNames.length; ii++) {
38+
props[actionNames[ii]] = component[actionNames[ii]];
39+
}
40+
41+
return {
42+
...props,
43+
...attrs
44+
};
45+
}
46+
47+
/**
48+
* 1. utilities are moved above because vue stores methods, states and props
49+
* in the same namespace
50+
* 2. actions are set while created
51+
*/
52+
53+
/**
54+
* @param mapStateToProps
55+
* @param mapActionsToProps
56+
* @returns Object
57+
*/
58+
export default function connect(mapStateToProps = noop, mapActionsToProps = noop) {
59+
return (children) => {
60+
61+
/** @namespace children.collect */
62+
if (children.collect) {
63+
console.warn('vua-redux: collect is deprecated, use props');
64+
children.props = {
65+
...normalizeProps(children.props || {}),
66+
...normalizeProps(children.collect || {})
67+
}
68+
}
69+
70+
return {
71+
name: `ConnectVuaRedux-${children.name || 'children'}`,
72+
73+
render(h) {
74+
const props = getProps(this);
75+
76+
return h(children, { props });
77+
},
78+
79+
data() {
80+
const state = getStates(this, mapStateToProps);
81+
const actions = getActions(this, mapActionsToProps);
82+
const stateNames = Object.keys(state);
83+
const actionNames = Object.keys(actions);
1684

1785
return {
18-
name: 'VuaRedux',
19-
20-
props: props,
21-
22-
render(h) {
23-
const keys = Object.keys(allProps);
24-
let propsToPass = {};
25-
for (let i = 0; i < keys.length; i++) {
26-
propsToPass[keys[i]] = this[keys[i]];
27-
}
28-
29-
return h(children, {
30-
props: propsToPass
31-
})
32-
},
33-
34-
data() {
35-
const store = this.$store;
36-
const state = mapStateAsProps(store.getState()) || {};
37-
const actions = mapActionsAsProps(store.dispatch, store.getState) || {};
38-
39-
return {
40-
...state,
41-
...actions
42-
};
43-
},
44-
45-
created() {
46-
const store = this.$store;
47-
const state = mapStateAsProps(store.getState()) || {};
48-
const stateNames = Object.keys(state);
49-
50-
this.unsubscribe = store.subscribe(() => {
51-
const state = mapStateAsProps(store.getState());
52-
53-
for (let i = 0; i < stateNames.length; i++) {
54-
this[stateNames[i]] = state[stateNames[i]];
55-
}
56-
});
57-
},
58-
59-
beforeDestroy() {
60-
this.unsubscribe();
61-
}
86+
...state,
87+
...actions,
88+
vuaReduxStateNames: stateNames,
89+
vuaReduxActionNames: actionNames
6290
};
91+
},
92+
93+
created() {
94+
const store = getStore(this);
95+
96+
this.vuaReduxUnsubscribe = store.subscribe(() => {
97+
const state = getStates(this, mapStateToProps);
98+
const stateNames = Object.keys(state);
99+
this.vuaReduxStateNames = stateNames;
100+
101+
for (let ii = 0; ii < stateNames.length; ii++) {
102+
this[stateNames[ii]] = state[stateNames[ii]];
103+
}
104+
});
105+
},
106+
107+
beforeDestroy() {
108+
this.vuaReduxUnsubscribe();
109+
}
63110
};
111+
};
64112
}

0 commit comments

Comments
 (0)