-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs_vue.pyj
102 lines (85 loc) · 3.55 KB
/
rs_vue.pyj
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#rs_vue.pyj
#------------ universal tricky @decorator ------------------
def _reg_as(name_or_fun, reg_as):
_name = None
_reg_as = reg_as
_inner_ = def(f):
f[_reg_as] = _name or f.name
return(f)
if isinstance( name_or_fun, Function):
return _inner_(name_or_fun)
_name = name_or_fun
return(_inner_)
#------------ @v_<decorators> ------------------
# usage: @v_meth or @v_meth('alias_name')
def v_meth(name_or_fun):
return _reg_as(name_or_fun, '_vue_meth')
def v_computed(name_or_fun):
return _reg_as(name_or_fun, '_vue_computed')
def v_filter(name_or_fun):
return _reg_as(name_or_fun, '_vue_filter')
def v_directive(name_or_fun):
return _reg_as(name_or_fun, '_vue_directive')
def v_watch(name_or_fun):
return _reg_as(name_or_fun, '_vue_watch')
def v_rstore_getter(name_or_fun):
return _reg_as(name_or_fun, '_vue_rstore_getter')
def v_rstore_mutation(name_or_fun):
return _reg_as(name_or_fun, '_vue_rstore_mutation')
def v_rstore_action(name_or_fun):
return _reg_as(name_or_fun, '_vue_rstore_action')
def v_collect(from_obj, to_obj, map, redefine):
#map = {_vue_meth: 'methods', ...}
for k in Object.getOwnPropertyNames(from_obj):
for reg_attr in map:
reg_name = from_obj[k][reg_attr]
if reg_name:
hash_name = map[reg_attr]
if not to_obj[hash_name]:
to_obj[hash_name] = {}
if to_obj[hash_name][reg_name] == undefined or redefine:
if hash_name == 'watch' and isinstance(reg_name, Object):
watcher_name = reg_name.name or from_obj[k].name
if watcher_name.startsWith('$w_'):
watcher_name= watcher_name.substr(3)
watcher_def = Object.assign({}, reg_name)
del watcher_def.name
watcher_def.handler = from_obj[k]
to_obj[hash_name][watcher_name] = watcher_def
else:
to_obj[hash_name][reg_name] = from_obj[k]
else:
raise Error('"' + reg_name + '" already exists in "' + hash_name + '", try v_collect(..., redefine = true)' )
break
#-------------------RS_vue-------------------
# simple class that collects all and converts to Vue-suitable obj
class RS_vue:
_map_attr_ = {
_vue_meth: 'methods',
_vue_computed: 'computed',
_vue_watch: 'watch',
_vue_filter: 'filters',
_vue_directive: 'directives',
_vue_rstore_mutation: 'mutations',
_vue_rstore_action: 'actions'
}
def __init__(self):
self.data = self._init_data
self.methods = {}
self.computed = {}
self.props = {}
self.watch = {}
#register methods, computed ...
v_collect(self.__proto__, self, self._map_attr_ )
#collect static method
v_collect(self.__proto__.constructor, self, self._map_attr_ )
self_keys = Object.keys(self)
for v_spec in [ 'beforeCreate','created',
'beforeMount', 'mounted',
'beforeUpdate', 'updated',
'activated', 'deactivated',
'beforeDestroy', 'destroyed']:
if !(v_spec in self_keys) and (tmp = self.__proto__[v_spec]):
self[v_spec] = tmp
def _init_data(self):
raise ReferenceError('Not implemented')