redux-vue 0.8.0 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +2 -1
- package/lib/connect.js +135 -0
- package/lib/index.js +20 -0
- package/lib/normalizeProps.js +40 -0
- package/lib/reduxStorePlugin.js +19 -0
- package/package.json +3 -1
package/.babelrc
CHANGED
package/lib/connect.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = connect;
|
|
7
|
+
var _normalizeProps = _interopRequireDefault(require("./normalizeProps"));
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
function noop() {}
|
|
10
|
+
function getStore(component) {
|
|
11
|
+
return component.$store;
|
|
12
|
+
}
|
|
13
|
+
function getAttrs(component) {
|
|
14
|
+
// issue #6: guard against missing attrs (root component has no _parentVnode attrs)
|
|
15
|
+
const vnode = component._self.$options._parentVnode;
|
|
16
|
+
return vnode && vnode.data && vnode.data.attrs || {};
|
|
17
|
+
}
|
|
18
|
+
function getStates(component, mapStateToProps) {
|
|
19
|
+
const store = getStore(component);
|
|
20
|
+
const attrs = getAttrs(component);
|
|
21
|
+
return mapStateToProps(store.getState(), attrs) || {};
|
|
22
|
+
}
|
|
23
|
+
function getActions(component, mapActionsToProps) {
|
|
24
|
+
const store = getStore(component);
|
|
25
|
+
return mapActionsToProps(store.dispatch, getAttrs.bind(null, component)) || {};
|
|
26
|
+
}
|
|
27
|
+
function getProps(component) {
|
|
28
|
+
const attrs = getAttrs(component);
|
|
29
|
+
const stateNames = component.vuaReduxStateNames;
|
|
30
|
+
const actionNames = component.vuaReduxActionNames;
|
|
31
|
+
const mergedNames = component.vuaReduxMergedNames;
|
|
32
|
+
|
|
33
|
+
// mergeProps path: only expose merged keys
|
|
34
|
+
if (mergedNames) {
|
|
35
|
+
const props = {};
|
|
36
|
+
for (let ii = 0; ii < mergedNames.length; ii++) {
|
|
37
|
+
props[mergedNames[ii]] = component[mergedNames[ii]];
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
...props,
|
|
41
|
+
...attrs
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const props = {};
|
|
45
|
+
for (let ii = 0; ii < stateNames.length; ii++) {
|
|
46
|
+
props[stateNames[ii]] = component[stateNames[ii]];
|
|
47
|
+
}
|
|
48
|
+
for (let ii = 0; ii < actionNames.length; ii++) {
|
|
49
|
+
props[actionNames[ii]] = component[actionNames[ii]];
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
...props,
|
|
53
|
+
...attrs
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param mapStateToProps
|
|
59
|
+
* @param mapActionsToProps
|
|
60
|
+
* @param mergeProps - optional; receives (stateProps, dispatchProps) -> props
|
|
61
|
+
* @returns Function (children) => VueComponentDescriptor
|
|
62
|
+
*/
|
|
63
|
+
function connect(mapStateToProps, mapActionsToProps, mergeProps) {
|
|
64
|
+
mapStateToProps = mapStateToProps || noop;
|
|
65
|
+
mapActionsToProps = mapActionsToProps || noop;
|
|
66
|
+
return children => {
|
|
67
|
+
/** @namespace children.collect */
|
|
68
|
+
if (children.collect) {
|
|
69
|
+
children.props = {
|
|
70
|
+
...(0, _normalizeProps.default)(children.props || {}),
|
|
71
|
+
...(0, _normalizeProps.default)(children.collect || {})
|
|
72
|
+
};
|
|
73
|
+
const msg = `vua-redux: collect is deprecated, use props ` + `in ${children.name || 'anonymous'} component`;
|
|
74
|
+
console.warn(msg);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
name: `ConnectVuaRedux-${children.name || 'children'}`,
|
|
78
|
+
render(h) {
|
|
79
|
+
const props = getProps(this);
|
|
80
|
+
// issue #3: forward $slots to child
|
|
81
|
+
return h(children, {
|
|
82
|
+
props,
|
|
83
|
+
slots: this.$slots
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
data() {
|
|
87
|
+
const state = getStates(this, mapStateToProps);
|
|
88
|
+
const actions = getActions(this, mapActionsToProps);
|
|
89
|
+
if (mergeProps) {
|
|
90
|
+
// PR #5: mergeProps support
|
|
91
|
+
const merged = mergeProps(state, actions) || {};
|
|
92
|
+
const mergedNames = Object.keys(merged);
|
|
93
|
+
return {
|
|
94
|
+
...merged,
|
|
95
|
+
vuaReduxMergedNames: mergedNames,
|
|
96
|
+
vuaReduxStateNames: [],
|
|
97
|
+
vuaReduxActionNames: []
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const stateNames = Object.keys(state);
|
|
101
|
+
const actionNames = Object.keys(actions);
|
|
102
|
+
return {
|
|
103
|
+
...state,
|
|
104
|
+
...actions,
|
|
105
|
+
vuaReduxStateNames: stateNames,
|
|
106
|
+
vuaReduxActionNames: actionNames
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
created() {
|
|
110
|
+
const store = getStore(this);
|
|
111
|
+
this.vuaReduxUnsubscribe = store.subscribe(() => {
|
|
112
|
+
const state = getStates(this, mapStateToProps);
|
|
113
|
+
const actions = getActions(this, mapActionsToProps);
|
|
114
|
+
if (mergeProps) {
|
|
115
|
+
const merged = mergeProps(state, actions) || {};
|
|
116
|
+
const mergedNames = Object.keys(merged);
|
|
117
|
+
this.vuaReduxMergedNames = mergedNames;
|
|
118
|
+
for (let ii = 0; ii < mergedNames.length; ii++) {
|
|
119
|
+
this[mergedNames[ii]] = merged[mergedNames[ii]];
|
|
120
|
+
}
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const stateNames = Object.keys(state);
|
|
124
|
+
this.vuaReduxStateNames = stateNames;
|
|
125
|
+
for (let ii = 0; ii < stateNames.length; ii++) {
|
|
126
|
+
this[stateNames[ii]] = state[stateNames[ii]];
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
beforeDestroy() {
|
|
131
|
+
this.vuaReduxUnsubscribe();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "connect", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _connect.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "reduxStorePlugin", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _reduxStorePlugin.default;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
var _connect = _interopRequireDefault(require("./connect"));
|
|
19
|
+
var _reduxStorePlugin = _interopRequireDefault(require("./reduxStorePlugin"));
|
|
20
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = normalizeProps;
|
|
7
|
+
const isArray = Array.isArray;
|
|
8
|
+
const isPlainObject = v => v !== null && typeof v === 'object' && Object.getPrototypeOf(v) === Object.prototype;
|
|
9
|
+
|
|
10
|
+
// https://github.com/vuejs/vue/blob/dev/src/util/options.js
|
|
11
|
+
function normalizeProps(props) {
|
|
12
|
+
var i,
|
|
13
|
+
val,
|
|
14
|
+
normalizedProps = {};
|
|
15
|
+
if (isArray(props)) {
|
|
16
|
+
i = props.length;
|
|
17
|
+
while (i--) {
|
|
18
|
+
val = props[i];
|
|
19
|
+
if (typeof val === 'string') {
|
|
20
|
+
normalizedProps[val] = null;
|
|
21
|
+
} else if (val.name) {
|
|
22
|
+
normalizedProps[val.name] = val;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
} else if (isPlainObject(props)) {
|
|
26
|
+
var keys = Object.keys(props);
|
|
27
|
+
i = keys.length;
|
|
28
|
+
while (i--) {
|
|
29
|
+
let key = keys[i];
|
|
30
|
+
val = props[key];
|
|
31
|
+
normalizedProps[key] = props[key];
|
|
32
|
+
if (typeof val === 'function') {
|
|
33
|
+
normalizedProps[key] = {
|
|
34
|
+
type: val
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return normalizedProps;
|
|
40
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = reduxStorePlugin;
|
|
7
|
+
function reduxStorePlugin(Vue, storeId = 'store') {
|
|
8
|
+
Vue.mixin({
|
|
9
|
+
beforeCreate() {
|
|
10
|
+
const options = this.$options;
|
|
11
|
+
// store injection
|
|
12
|
+
if (options[storeId]) {
|
|
13
|
+
this.$store = options.store;
|
|
14
|
+
} else if (options.parent && options.parent.$store) {
|
|
15
|
+
this.$store = options.parent.$store;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "redux-vue",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"description": "Vue Redux binding higher order component",
|
|
5
5
|
"author": "Nadim Tuhin",
|
|
6
6
|
"repository": {
|
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
"immutable"
|
|
27
27
|
],
|
|
28
28
|
"devDependencies": {
|
|
29
|
+
"@babel/cli": "^8.0.1",
|
|
29
30
|
"@babel/core": "^8.0.1",
|
|
31
|
+
"@babel/plugin-proposal-export-default-from": "^8.0.1",
|
|
30
32
|
"@babel/preset-env": "^8.0.2",
|
|
31
33
|
"@babel/register": "^8.0.1",
|
|
32
34
|
"expect": "^1.20.2",
|