@@ -58,42 +58,55 @@ Instructions:
58
58
4 . Move your existing code responsible for initializing your application to the ` constructor ` of the ` View ` class.
59
59
5 . The bulk of your remaining code should probably go to the ` fetchAndRender() ` method of the ` View ` class.
60
60
61
- ### Skeleton
61
+ ### Suggested skeleton
62
62
63
- Use this skeleton as overall design for your code in ` app.js ` :
63
+ You could use this skeleton as overall design for your code in ` app.js ` :
64
64
65
65
``` js
66
66
' use strict' ;
67
67
{
68
68
const hyfUrl = ' https://api.github.com' ;
69
69
const hyfReposUrl = hyfUrl + ' /orgs/HackYourFuture/repos?per_page=100' ;
70
- const repoUrl = hyfUrl + ' /repos/HackYourFuture/' ;
71
70
72
71
class Repository {
73
72
constructor (data ) {
74
- this .data = data;
73
+ this ._data = data;
75
74
}
76
75
77
76
/**
78
- * Render the repository info into the DOM.
79
- * @param {HTML element} parent The parent element in which to render the repository
77
+ * Render the repository info to the DOM.
78
+ * @param {HTML element} parent The parent element in which to render the repository.
80
79
* info.
81
80
*/
82
81
render (parent ) {
83
82
// Add your code here.
84
- // This method should render the repository data stored in the 'data ' property
83
+ // This method should render the repository data stored in the '_data ' property
85
84
// as HTML elements and append them to the `parent` element.
86
85
}
86
+
87
+ /**
88
+ * Returns an array of contributors as a promise
89
+ */
90
+ fetchContributors () {
91
+ // Add your code here
92
+ }
93
+
94
+ /**
95
+ * Returns the name of the repository
96
+ */
97
+ name () {
98
+ // Add your code here
99
+ }
87
100
}
88
101
89
102
class Contributor {
90
103
constructor (data ) {
91
- this .data = data;
104
+ this ._data = data;
92
105
}
93
106
94
107
/**
95
- * Render the contributor info into the DOM.
96
- * @param {HTML element} parent The parent element in which to render the contributor
108
+ * Render the contributor info to the DOM.
109
+ * @param {HTML element} parent The parent element in which to render the contributor.
97
110
* info.
98
111
*/
99
112
render (parent ) {
@@ -105,6 +118,13 @@ Use this skeleton as overall design for your code in `app.js`:
105
118
106
119
class View {
107
120
constructor () {
121
+ this .initialize ();
122
+ }
123
+
124
+ /**
125
+ * View initialization
126
+ */
127
+ async initialize () {
108
128
// Add code here to initialize your app
109
129
// 1. Create the fixed HTML elements of your page
110
130
// 2. Make an initial XMLHttpRequest to populate your <select> element
@@ -115,29 +135,24 @@ Use this skeleton as overall design for your code in `app.js`:
115
135
* information as HTML elements in the DOM
116
136
* @param {*} repoName The name of the selected repository
117
137
*/
118
- fetchAndRender (repoName ) {
138
+ async fetchAndRender (repoName ) {
119
139
const leftDiv = ... ;
120
140
const rightDiv = ... ;
121
141
122
142
// ...
123
143
124
- this .fetchJSON (repoUrl + repoName)
125
- .then (repoInfo => {
126
- const repo = new Repository (repoInfo);
127
- return this .fetchJSON (repoInfo .contributors_url )
128
- .then (contributors => {
129
- repo .render (leftDiv);
130
- contributors
131
- .map (contributor => new Contributor (contributor))
132
- .forEach (contributor => contributor .render (rightDiv));
133
- });
134
- })
135
- .catch (error => {
136
- // add error handling code here
137
- });
144
+ try {
145
+ const contributors = await repo .fetchContributors ();
146
+ repo .render (leftDiv);
147
+ contributors
148
+ .map (contributor => new Contributor (contributor))
149
+ .forEach (contributor => contributor .render (contributorList));
150
+ }
151
+ catch (error) {
152
+ createAndAppend (' div' , container, { html: error .message , class: ' alert alert-error' });
153
+ }
138
154
}
139
155
140
-
141
156
/**
142
157
* Fetch API data as JSON data in a promise
143
158
* @param {string} url The URL of the API endpoint.
0 commit comments