Skip to content

Commit 8cce0aa

Browse files
committed
Added statics & refactored
1 parent 2d308c7 commit 8cce0aa

File tree

5 files changed

+125
-37
lines changed

5 files changed

+125
-37
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ export default {
3131

3232
````
3333

34+
### Statics
35+
36+
Vue-Sap includes statics for components. This can let you share and modify the same value... statics are usually frowned upon though... perhaps you shouldn't use them.
37+
38+
````javascript
39+
export default{
40+
statics:{
41+
styleObj:{backgroundColor:'#aaa'}
42+
},
43+
methods:{
44+
onClick(){
45+
const so = this.styleObj;
46+
if(so.backgroundColor === 'red'){
47+
so.backgroundColor = 'blue';
48+
}else{
49+
so.backgroundColor = 'red';
50+
}
51+
}
52+
},
53+
template: `<div :style="styleObj" @click="onClick" style="width:40px; height:40px; border-radius: 20px 20px;"></div>`
54+
}
55+
56+
````
3457

3558
#### The Output
3659

example/example.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<div id="app">
77

88
<test-component></test-component>
9+
<static-component></static-component>
10+
<static-component></static-component>
11+
<static-component></static-component>
912
</div>
1013

1114

@@ -21,6 +24,24 @@
2124

2225
})
2326

27+
28+
Vue.component('static-component',{
29+
statics:{
30+
styleObj:{backgroundColor:'#aaa'}
31+
},
32+
methods:{
33+
onClick(){
34+
const so = this.styleObj;
35+
if(so.backgroundColor === 'red'){
36+
so.backgroundColor = 'blue';
37+
}else{
38+
so.backgroundColor = 'red';
39+
}
40+
}
41+
},
42+
template: `<div :style="styleObj" @click="onClick" style="width:40px; height:40px; border-radius: 20px 20px;"></div>`
43+
});
44+
2445
new Vue({
2546
el:'#app',
2647
published:{

src/main.js

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
1-
const plugin = {
1+
import createStore from './store';
2+
import mixin from './mixin';
3+
const subscriptions = createStore(Vue);
24

5+
const plugin = {
36
install(Vue){
4-
let ss = Vue.prototype.$Subscriptions = new Vue({data:{subscriptions:{}}}).subscriptions;
5-
6-
Vue.mixin({
7-
created(){
8-
const p = this.$options.published;
9-
if(p){
10-
for(let key in p){
11-
Vue.set(ss, key, p[key]);
12-
Object.defineProperty(this, key,{
13-
get(){
14-
return ss[key];
15-
},
16-
set(newValue){
17-
Vue.set(ss, key, newValue);
18-
}
19-
});
20-
}
21-
}
22-
const s = this.$options.subscribed;
23-
if(s){
24-
for(let key in s){
25-
26-
Object.defineProperty(this, key,{
27-
get(){
28-
return ss[key] || '';
29-
},
30-
set(newValue){
31-
Vue.set(ss, key, newValue);
32-
}
33-
});
34-
}
35-
this.$forceUpdate();
36-
}
37-
}
38-
});
7+
Vue.mixin(mixin);
398
}
40-
}
9+
};
4110

4211

4312
if(window !== undefined && window.Vue){

src/mixin.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
function hasProp(obj, prop){
3+
if(obj.hasOwnProperty){
4+
return obj.hasOwnProperty(prop);
5+
}else{
6+
return !!obj[prop];
7+
}
8+
}
9+
10+
function definePublishedProperty(vm, key, obj){
11+
const ss = vm.$subscriptions;
12+
vm.$set(ss, key, obj[key]);
13+
Object.defineProperty(vm, key,{
14+
get: ()=> ss[key],
15+
set: (newValue)=> {vm.$set(ss, key, newValue);}
16+
});
17+
}
18+
19+
20+
function defineSubscribedProperty(vm, key, obj){
21+
const ss = vm.$subscriptions;
22+
Object.defineProperty(vm, key,{
23+
get:()=> ss[key],
24+
set:(newValue)=> { vm.$set(ss, key, newValue); }
25+
});
26+
}
27+
28+
function defineStaticProperty(vm, key, obj){
29+
const ss = vm.$statics;
30+
const cid = vm.constructor.cid;
31+
if(!ss[cid]){
32+
vm.$set(ss, cid, {});
33+
}
34+
if(!hasProp(ss[cid], key)){
35+
vm.$set(ss[cid], key, obj[key]);
36+
}
37+
Object.defineProperty(vm, key,{
38+
get:()=>ss[cid][key],
39+
set:(newValue)=> {vm.$set(ss[cid], key, newValue);}
40+
});
41+
}
42+
43+
export default {
44+
created(){
45+
const { published, subscribed, statics } = this.$options;
46+
if(published){
47+
for(let key in published){
48+
definePublishedProperty(this, key, published);
49+
}
50+
}
51+
52+
if(subscribed){
53+
for(let key in subscribed){
54+
defineSubscribedProperty(this, key, subscribed);
55+
}
56+
this.$forceUpdate();
57+
}
58+
59+
if(statics){
60+
for(let key in statics){
61+
defineStaticProperty(this, key, statics);
62+
}
63+
this.$forceUpdate();
64+
}
65+
66+
}
67+
};

src/store.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
function createStore(Vue){
3+
const store = new Vue({data:{subscriptions:{}, statics:{}}});
4+
Vue.prototype.$subscriptions = store.subscriptions;
5+
Vue.prototype.$statics = store.statics;
6+
}
7+
8+
export default createStore;

0 commit comments

Comments
 (0)