Skip to content

Commit 3bae1d9

Browse files
committed
first commit
1 parent 605dc89 commit 3bae1d9

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

Mvc.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
class Mvc {
2+
static instance = null;
3+
4+
constructor() {
5+
this.controllers = {}
6+
this.models = {};
7+
}
8+
9+
static getInstance() {
10+
if (!Mvc.instance)
11+
Mvc.instance = new Mvc();
12+
return Mvc.instance;
13+
}
14+
15+
newController(name) {
16+
var controller = new Controller();
17+
this.addController(name, controller);
18+
return controller;
19+
}
20+
21+
addController(name, controller) {
22+
this.controllers[name] = controller;
23+
}
24+
25+
getController(name) {
26+
var controller = this.controllers[name];
27+
return controller;
28+
}
29+
}
30+
31+
class Controller {
32+
33+
constructor() {
34+
this.views = {};
35+
}
36+
37+
addView(action, viewId, view) {
38+
var available = this.views[action];
39+
if (!available) {
40+
available = {};
41+
}
42+
available[viewId] = view;
43+
this.views[action] = available;
44+
}
45+
46+
addDefaultView(action, view) {
47+
this.addView(action, "", view);
48+
}
49+
50+
removeView(action, viewId) {
51+
var available = this.views[action];
52+
if (available) {
53+
delete available[viewId];
54+
}
55+
}
56+
57+
removeDefaultView(action) {
58+
this.removeView(action, "");
59+
}
60+
61+
removeViews(action) {
62+
var available = this.views[action];
63+
if (available) {
64+
for (var key in available)
65+
delete available[key];
66+
}
67+
delete this.views[action];
68+
}
69+
70+
removeAllViews() {
71+
for(var action in this.views) {
72+
var available = this.views[action];
73+
for (var key in available)
74+
delete available[key];
75+
delete this.views[action];
76+
}
77+
}
78+
79+
updateView(action, viewId, data) {
80+
var available = this.views[action];
81+
if (available) {
82+
var view = available[viewId];
83+
if(view)
84+
view(data);
85+
}
86+
}
87+
88+
updateViews(action, data) {
89+
var available = this.views[action];
90+
if (available) {
91+
for (var key in available) {
92+
var view = available[key];
93+
view(data);
94+
}
95+
}
96+
}
97+
}
98+
99+
export default Mvc

0 commit comments

Comments
 (0)