Skip to content

Commit 8a9cc64

Browse files
committed
Updated
1 parent aadb811 commit 8a9cc64

File tree

5 files changed

+100
-54
lines changed

5 files changed

+100
-54
lines changed

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"editor.codeActionsOnSave": {
3+
"source.fixAll.eslint": true
4+
},
5+
"eslint.validate": [
6+
"javascript"
7+
]
8+
}

html/index.html

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,29 @@ <h1>Tests</h1>
3636
"arr": "[]StrModel",
3737
"override": "string",
3838
});
39-
40-
var obj = new KeyModel({ _id: "[this is the key]", map: { a: 1, b: 2 }, arr: [ "a", "b" ], override: "not done" });
41-
obj.$type.forEach((v,k) =>{
42-
console.log(`${k} => `,v);
39+
40+
var obj = new KeyModel({ _id: "[this is the key]", map: { a: 1, b: 2 }, arr: ["a", "b"], override: "not done" });
41+
obj.$type.forEach((v, k) => {
42+
console.log(`${k} => `, v);
43+
});
44+
console.log("className=", obj.$className);
45+
console.log("getClassName=", obj.getClassName());
46+
console.log("str=", obj.str);
47+
console.log("map=", obj.map);
48+
console.log("arr=", obj.arr);
49+
console.log("override=", obj.override);
50+
console.log("toString=", "" + obj);
51+
52+
var provider = new mvc.Provider();
53+
provider.addEventListener('mvc.provider.completed', (sender) => {
54+
console.log("Completed", sender);
55+
});
56+
provider.addEventListener('mvc.provider.error', (sender, error) => {
57+
console.log("Error", error);
58+
});
59+
provider.fetch('https://rpi4b.mutablelogic.com/', {
60+
mode: 'no-cors',
4361
});
44-
console.log("className=",obj.$className);
45-
console.log("getClassName=",obj.getClassName());
46-
console.log("str=",obj.str);
47-
console.log("map=",obj.map);
48-
console.log("arr=",obj.arr);
49-
console.log("override=",obj.override);
50-
console.log("toString=","" + obj);
5162
</script>
5263
</body>
5364

js/events.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Emitter class
2+
3+
export default class Emitter {
4+
constructor() {
5+
this.$listeners = new Map();
6+
}
7+
8+
addEventListener(type, fn) {
9+
if (!this.$listeners.has(type)) {
10+
this.$listeners.set(type, []);
11+
}
12+
this.$listeners.get(type).push(fn);
13+
}
14+
15+
dispatchEvent(type, ...args) {
16+
const listeners = this.$listeners.get(type);
17+
if (listeners) {
18+
listeners.forEach((fn) => {
19+
fn(...args);
20+
});
21+
}
22+
}
23+
}

js/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import Model from './model';
22
import View from './view';
33
import Provider from './provider';
44
import Error from './error';
5+
import Emitter from './events';
56

67
export {
7-
Model, View, Provider, Error,
8+
Model, View, Provider, Error, Emitter,
89
};

js/provider.js

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,62 @@
11
// Provider class to be subclassed by an actual provider
22

33
import Error from './error';
4+
import Emitter from './events';
45

56
// ////////////////////////////////////////////////////////////////////////////
67
// CONSTANTS
78

8-
const EVENT_COMPLETED = "mvc.provider.completed";
9-
const EVENT_ERROR = "mvc.provider.error";
9+
const EVENT_ROOT = 'mvc.provider';
10+
const EVENT_COMPLETED = `${EVENT_ROOT}.completed`;
11+
const EVENT_ERROR = `${EVENT_ROOT}.error`;
1012

1113
// ////////////////////////////////////////////////////////////////////////////
1214
// PROVIDER CLASS
1315

14-
export default class Provider {
16+
export default class Provider extends Emitter {
1517
constructor(root) {
16-
this.$root = root;
18+
super();
19+
this.$root = root || '';
1720
}
18-
19-
$fetch(url, req, userInfo) {
20-
var status;
21-
window.fetch(this.$root + url, req).then(response => {
22-
status = response;
23-
switch (status.headers.get("Content-Type").split(";")[0]) {
24-
case "application/json":
25-
case "text/json":
26-
return response.json();
27-
case "text/plain":
28-
return response.text();
29-
default:
30-
return response.blob();
31-
}
32-
}).then(data => {
33-
if (!status.ok) {
34-
if (typeof (data) == "object" && data.reason) {
35-
throw new Error(data.reason, data.code);
21+
22+
fetch(url, req, userInfo) {
23+
let status;
24+
fetch(this.$root + url, req)
25+
.then((response) => {
26+
status = response;
27+
const contentType = response.headers ? response.headers.get('Content-Type') || '' : '';
28+
switch (contentType.split(';')[0]) {
29+
case 'application/json':
30+
case 'text/json':
31+
return response.json();
32+
case 'text/plain':
33+
return response.text();
34+
default:
35+
return response.blob();
36+
}
37+
})
38+
.then((data) => {
39+
if (!status.ok) {
40+
if (typeof (data) === 'object' && data.reason) {
41+
throw new Error(data.reason, data.code);
42+
} else {
43+
throw new Error(status.statusText, status.status);
44+
}
45+
} else if (typeof (data) === 'object' && Array.isArray(data)) {
46+
this.$array(data);
47+
} else {
48+
this.$object(data);
49+
}
50+
})
51+
.then(() => {
52+
this.dispatchEvent(EVENT_COMPLETED, this, userInfo);
53+
})
54+
.catch((error) => {
55+
if (error instanceof Error) {
56+
this.dispatchEvent(EVENT_ERROR, this, error, userInfo);
3657
} else {
37-
throw new Error(status.statusText, status.status);
58+
throw error;
3859
}
39-
} else if (typeof (data) == "object" && Array.isArray(data)) {
40-
this.$array(data);
41-
} else {
42-
this.$object(data);
43-
}
44-
}).then(() => {
45-
document.dispatchEvent(new Event(EVENT_COMPLETED, {
46-
userInfo: userInfo,
47-
}));
48-
}).catch(error => {
49-
if (error instanceof Error) {
50-
document.dispatchEvent(new Event(EVENT_ERROR, {
51-
userInfo: userInfo,
52-
error: error,
53-
}));
54-
} else {
55-
throw error;
56-
}
57-
});
60+
});
5861
}
5962
}

0 commit comments

Comments
 (0)