Skip to content

Commit e195998

Browse files
author
Lee Richmond
committed
Throw more helpful error when class not found
1 parent dd73037 commit e195998

File tree

5 files changed

+362
-36
lines changed

5 files changed

+362
-36
lines changed

dist/jsorm.js

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/jsorm.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 330 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,332 @@
11
<html>
2-
<head>
3-
<meta charset="utf-8">
4-
<title>Test</title>
5-
</head>
6-
<body>
7-
<script src="dist/jsorm.js?v=asfasfdsf"></script>
8-
<script>
9-
var Model = jsorm.Model;
10-
var attr = jsorm.attr;
11-
var Config = jsorm.Config;
12-
13-
const Staffer = Model.extend({
14-
static: {
15-
jsonapiType: 'staffers',
16-
apiNamespace: '/api/v1',
17-
baseUrl: ''
18-
},
19-
20-
firstName: attr(),
21-
lastName: attr(),
22-
fullName() {
23-
return this.firstName + ' ' + this.lastName;
24-
}
25-
});
26-
27-
Staffer.find(1297466).then((s) => {
28-
console.log(s);
29-
console.log(s.fullName());
30-
});
31-
32-
Config.setup();
33-
</script>
34-
</body>
2+
<head>
3+
<meta charset="utf-8">
4+
<title>Test</title>
5+
</head>
6+
<body>
7+
<script src="dist/jsorm.js"></script>
8+
<script data-require="jquery@*" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
9+
<script>
10+
var Model = jsorm.Model;
11+
var attr = jsorm.attr;
12+
var Config = jsorm.Config;
13+
var hasOne = jsorm.hasOne;
14+
var hasMany = jsorm.hasMany;
15+
var belongsTo = jsorm.belongsTo;
16+
17+
const ApplicationRecord = Model.extend({
18+
static: {
19+
baseUrl: '',
20+
apiNamespace: '/api/v1'
21+
}
22+
});
23+
24+
const Staffer = ApplicationRecord.extend({
25+
static: {
26+
jsonapiType: 'staffers',
27+
},
28+
29+
positions: hasMany('positions'),
30+
currentPosition: hasOne('positions'),
31+
socialMediaAccounts: hasMany('social_media_accounts'),
32+
educationalExperiences: hasMany('educational_experiences'),
33+
issues: hasMany(),
34+
hobbies: hasMany(),
35+
traits: hasMany(),
36+
37+
firstName: attr(),
38+
lastName: attr(),
39+
title: attr(),
40+
email: attr(),
41+
fullName() {
42+
return this.firstName + ' ' + this.lastName;
43+
},
44+
// todo memoize/computed
45+
position() {
46+
if (this.currentPosition) return this.currentPosition;
47+
return this.positions.sort(function(a, b) {
48+
return parseFloat(a.historicalIndex) - parseFloat(b.historicalIndex);
49+
})[0];
50+
},
51+
52+
_issues() {
53+
if (this.issues.length > 0) return this.issues;
54+
return this.traits.filter(function(t) {
55+
return t.klass.jsonapiType === 'issues';
56+
});
57+
},
58+
59+
issueList() {
60+
return this._issues().map(function(issue) {
61+
return issue.name;
62+
}).join(', ');
63+
}
64+
});
65+
66+
const Position = ApplicationRecord.extend({
67+
static: {
68+
jsonapiType: 'positions'
69+
},
70+
71+
office: hasOne('offices'),
72+
historicalIndex: attr(),
73+
organizationName: attr()
74+
});
75+
76+
const LegislativePosition = Position.extend({
77+
static: {
78+
jsonapiType: 'legislative_positions'
79+
},
80+
81+
legislatorId: attr(),
82+
officeLink() {
83+
return 'https://beta.bgov.com/us_legislators/BB' + this.legislatorId;
84+
}
85+
});
86+
87+
const CommitteePosition = Position.extend({
88+
static: {
89+
jsonapiType: 'committee_positions'
90+
},
91+
92+
committeeId: attr(),
93+
officeLink() {
94+
return 'https://beta.bgov.com/us_committees/BB' + this.committeeId;
95+
}
96+
});
97+
98+
const Office = ApplicationRecord.extend({
99+
static: {
100+
jsonapiType: 'offices'
101+
},
102+
103+
name: attr(),
104+
building: attr(),
105+
city: attr(),
106+
state: attr(),
107+
zipCode: attr(),
108+
phone: attr(),
109+
110+
address() {
111+
return `${this.city}, ${this.state} ${this.zipCode}`;
112+
}
113+
});
114+
115+
const SocialMediaAccount = ApplicationRecord.extend({
116+
static: {
117+
jsonapiType: 'social_media_accounts'
118+
},
119+
120+
path: attr()
121+
});
122+
123+
const LegislativeDetail = ApplicationRecord.extend({
124+
static: {
125+
jsonapiType: 'legislative_details'
126+
},
127+
128+
titleLink: attr()
129+
});
130+
131+
const EducationalExperience = ApplicationRecord.extend({
132+
static: {
133+
jsonapiType: 'educational_experiences'
134+
},
135+
136+
degree: attr(),
137+
schoolName: attr(),
138+
endedAt: attr()
139+
});
140+
141+
const Trait = ApplicationRecord.extend({
142+
static: {
143+
jsonapiType: 'traits'
144+
},
145+
146+
name: attr()
147+
});
148+
149+
const Issue = Trait.extend({
150+
static: {
151+
jsonapiType: 'issues'
152+
}
153+
});
154+
155+
const Hobby = Trait.extend({
156+
static: {
157+
jsonapiType: 'hobbies'
158+
}
159+
})
160+
161+
Config.setup();
162+
163+
scope = Staffer
164+
.includes(['traits', 'educational_experiences', 'social_media_accounts', { positions: { office: 'legislative_detail' } }])
165+
.selectExtra({ staffers: ['email'] });
166+
167+
// Sample IDs
168+
// Basic - 1312133
169+
// Committee - 1303678
170+
// Issues, Hobbies - 1297114
171+
var id = window.location.href.split('?id=')[1];
172+
scope.find(id).then((s) => {
173+
$('.main-name').text(s.fullName());
174+
175+
window.s = s;
176+
var position = s.position();
177+
$('.main-title').text(position.title);
178+
$('.office .building').text(position.office.building);
179+
$('.office .address').text(position.office.address());
180+
$('.office .phone').text(position.office.phone);
181+
$('.email').text(s.email);
182+
console.log(s.issueList());
183+
$('.main-issues').text(s.issueList());
184+
185+
let positions = s.positions;
186+
for (var i = 0; i < positions.length; i++) {
187+
var position = positions[i];
188+
$($('.position .title').get(i)).text(position.title);
189+
190+
if (position.office) {
191+
$($('.position .office-link').get(i)).text(position.organizationName);
192+
$($('.position .office-link').get(i)).attr('href', position.officeLink());
193+
}
194+
}
195+
196+
let accounts = s.socialMediaAccounts;
197+
for (var i = 0; i < accounts.length; i++) {
198+
var account = accounts[i];
199+
$($('.social-media-account').get(i)).text(account.path);
200+
}
201+
202+
$('.office .link').text(position.organizationName);
203+
$ ('.office .link').attr('href', position.officeLink());
204+
205+
for (var i = 0; i < s.educationalExperiences.length; i++) {
206+
var exp = s.educationalExperiences[i];
207+
$($('.educational-experience .degree').get(i)).text(exp.degree);
208+
$($('.educational-experience .school-name').get(i)).text(exp.schoolName);
209+
$($('.educational-experience .ended-at').get(i)).text(exp.endedAt);
210+
}
211+
212+
for (var i = 0; i < s.hobbies.length; i++) {
213+
var hobby = s.hobbies[i];
214+
$($('.hobbies .hobby').get(i)).text(hobby.name);
215+
}
216+
});
217+
218+
// Staffer.includes(['issues', 'current_position']).selectExtra({ staffers: ['email'] }).where({ colleague_of: id }).all().then((staffers) => {
219+
// for (var i = 0; i < staffers.length; i++) {
220+
// var staffer = staffers[i];
221+
// $($('.colleague .colleague-name').get(i)).text(staffer.fullName());
222+
// $($('.colleague .colleague-email').get(i)).text(staffer.email);
223+
// $($('.colleague .colleague-title').get(i)).text(staffer.position().title);
224+
// $($('.colleague .colleague-issues').get(i)).text(staffer.issueList());
225+
// }
226+
// });
227+
</script>
228+
229+
<div class='main'>
230+
<div class='main-name'></div>
231+
<div class='main-title'></div>
232+
<div class='main-issues'></div>
233+
<div class='office'>
234+
<div class='building'></div>
235+
<div class='address'></div>
236+
<div class='phone'></div>
237+
</div>
238+
<div class='email'></div>
239+
240+
<br />
241+
<br />
242+
<br />
243+
<div class='positions'>
244+
<div class='position'>
245+
<div class='title'></div>
246+
<a class='office-link' href=''></a>
247+
</div>
248+
<div class='position'>
249+
<div class='title'></div>
250+
<a class='office-link' href=''></a>
251+
</div>
252+
<div class='position'>
253+
<div class='title'></div>
254+
<a class='office-link' href=''></a>
255+
</div>
256+
<div class='position'>
257+
<div class='title'></div>
258+
<a class='office-link' href=''></a>
259+
</div>
260+
<div class='position'>
261+
<div class='title'></div>
262+
<a class='office-link' href=''></a>
263+
</div>
264+
<div class='position'>
265+
<div class='title'></div>
266+
<a class='office-link' href=''></a>
267+
</div>
268+
<div class='position'>
269+
<div class='title'></div>
270+
<a class='office-link' href=''></a>
271+
</div>
272+
</div>
273+
<br />
274+
<br />
275+
<br />
276+
277+
<div class='colleagues'>
278+
<h3>Colleagues</h3>
279+
<div class='colleague'>
280+
<div class='colleague-name'></div>
281+
<div class='colleague-title'></div>
282+
<div class='colleague-email'></div>
283+
<a href='' class='colleague-office'></a>
284+
<div class='colleague-issues'></div>
285+
</div>
286+
</div>
287+
288+
<br />
289+
<br />
290+
<br />
291+
292+
<ul class='social-media-accounts'>
293+
<li class='social-media-account'></li>
294+
<li class='social-media-account'></li>
295+
<li class='social-media-account'></li>
296+
</ul>
297+
298+
<div class='office'>
299+
<div>Office:</div>
300+
<a class='link' href=''></a>
301+
</div>
302+
303+
<div class='hobbies'>
304+
<h3>Hobbies</h3>
305+
<div class='hobby'>
306+
<div class='name'></div>
307+
</div>
308+
</div>
309+
310+
<div class='educational-experiences'>
311+
<h3>Education</h3>
312+
<div class='educational-experience'>
313+
<div class='degree'></div>
314+
<div class='school-name'></div>
315+
<div class='ended-at'></div>
316+
</div>
317+
318+
<div class='educational-experience'>
319+
<div class='degree'></div>
320+
<div class='school-name'></div>
321+
<div class='ended-at'></div>
322+
</div>
323+
324+
<div class='educational-experience'>
325+
<div class='degree'></div>
326+
<div class='school-name'></div>
327+
<div class='ended-at'></div>
328+
</div>
329+
</div>
330+
</div>
331+
</body>
35332
</html>

src/configuration.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export default class Config {
2525
}
2626

2727
static modelForType(type: string) : typeof Model {
28-
return this.typeMapping[type];
28+
let klass = this.typeMapping[type];
29+
if (klass) {
30+
return klass;
31+
} else {
32+
throw(`Could not find class for jsonapi type "${type}"`)
33+
}
2934
}
3035
}

0 commit comments

Comments
 (0)