UNPKG

4.05 kBJavaScriptView Raw
1'use strict';
2
3var babelHelpers = {};
4
5babelHelpers.classCallCheck = function (instance, Constructor) {
6 if (!(instance instanceof Constructor)) {
7 throw new TypeError("Cannot call a class as a function");
8 }
9};
10
11babelHelpers.createClass = function () {
12 function defineProperties(target, props) {
13 for (var i = 0; i < props.length; i++) {
14 var descriptor = props[i];
15 descriptor.enumerable = descriptor.enumerable || false;
16 descriptor.configurable = true;
17 if ("value" in descriptor) descriptor.writable = true;
18 Object.defineProperty(target, descriptor.key, descriptor);
19 }
20 }
21
22 return function (Constructor, protoProps, staticProps) {
23 if (protoProps) defineProperties(Constructor.prototype, protoProps);
24 if (staticProps) defineProperties(Constructor, staticProps);
25 return Constructor;
26 };
27}();
28
29babelHelpers.slicedToArray = function () {
30 function sliceIterator(arr, i) {
31 var _arr = [];
32 var _n = true;
33 var _d = false;
34 var _e = undefined;
35
36 try {
37 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
38 _arr.push(_s.value);
39
40 if (i && _arr.length === i) break;
41 }
42 } catch (err) {
43 _d = true;
44 _e = err;
45 } finally {
46 try {
47 if (!_n && _i["return"]) _i["return"]();
48 } finally {
49 if (_d) throw _e;
50 }
51 }
52
53 return _arr;
54 }
55
56 return function (arr, i) {
57 if (Array.isArray(arr)) {
58 return arr;
59 } else if (Symbol.iterator in Object(arr)) {
60 return sliceIterator(arr, i);
61 } else {
62 throw new TypeError("Invalid attempt to destructure non-iterable instance");
63 }
64 };
65}();
66
67babelHelpers;
68
69// to valid and match like `a as x.y.z`
70var re = /^([a-zA-Z0-9_-]+)\s{1,2}as\s{1,2}([a-zA-Z0-9\._-]+)$/i;
71
72var isDev = process.env.NODE_ENV !== 'production';
73
74function parseProp(prop) {
75 // realProp: property name/path in your instance
76 // storeProp: property name/path in Redux store
77 var realProp = prop;
78 var storeProp = prop;
79 if (re.test(prop)) {
80 var _prop$match = prop.match(re);
81
82 var _prop$match2 = babelHelpers.slicedToArray(_prop$match, 3);
83
84 storeProp = _prop$match2[1];
85 realProp = _prop$match2[2];
86 }
87 return { storeProp: storeProp, realProp: realProp };
88}
89
90/**
91 * Bind reduxStore to Vue instance
92 *
93 * @param {Vue} Vue
94 * @param {object} store - redux store
95 */
96function bindVue(Vue, store) {
97 Vue.mixin({
98 created: function created() {
99 var _this = this;
100
101 if (this._bindProps) {
102 var handleChange = function handleChange() {
103 _this._bindProps.forEach(function (prop) {
104 var storeProp = prop.storeProp;
105 var realProp = prop.realProp;
106
107 if (realProp && storeProp) {
108 var currentValue = store.getState()[storeProp];
109 _this.$set(realProp, currentValue);
110 }
111 });
112 };
113 this._unsubscribe = store.subscribe(handleChange);
114 }
115 },
116 beforeDestroy: function beforeDestroy() {
117 if (this._unsubscribe) {
118 this._unsubscribe();
119 }
120 }
121 });
122 Vue.prototype.$select = function (prop) {
123 // realProp: property name/path in your instance
124 // storeProp: property name/path in Redux store
125 this._bindProps = this._bindProps || [];
126 prop = parseProp(prop);
127 this._bindProps.push(prop);
128 return store.getState()[prop.storeProp];
129 };
130}
131
132var Revue = function () {
133 function Revue(Vue, reduxStore, reduxActions) {
134 babelHelpers.classCallCheck(this, Revue);
135
136 this.store = reduxStore;
137 bindVue(Vue, this.store);
138 if (reduxActions) {
139 this.reduxActions = reduxActions;
140 }
141 }
142
143 babelHelpers.createClass(Revue, [{
144 key: 'dispatch',
145 value: function dispatch() {
146 var _store;
147
148 return (_store = this.store).dispatch.apply(_store, arguments);
149 }
150 }, {
151 key: 'state',
152 get: function get() {
153 return this.store.getState();
154 }
155 }, {
156 key: 'actions',
157 get: function get() {
158 if (isDev && !this.reduxActions) {
159 throw new Error('[Revue] Binding actions to Revue before calling them!');
160 }
161 return this.reduxActions;
162 }
163 }]);
164 return Revue;
165}();
166
167module.exports = Revue;
\No newline at end of file