Skip to content

Commit 898a18d

Browse files
committed
Rewrite Meetup class
This is more JavaScript class idiomatic. It also allows for future abstractions like removing the hard coded URLs. The original file used tabs so I kept that even though the .vimrc seems to contradict this. This also uses UMD for encapsulation which makes it more universal to be pulled into other websites.
1 parent 984a12f commit 898a18d

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

js/meetup-events.js

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
1-
'use strict';
2-
3-
var Meetup = function(meetupURL) {
4-
this.meetupURL = (typeof meetupURL!=='undefined') ? meetupURL :
5-
"https://api.meetup.com/2/events?offset=0&format=json&limited_events=False&group_urlname=techcorridorio&page=200&fields=&order=time&desc=false&status=upcoming&sig_id=168857872&sig=e659cc6038d27adf6eae600a44905c69196c77df";
6-
7-
this.getEvents = function(callback) {
8-
$.ajax({
9-
url: this.meetupURL,
10-
dataType: 'jsonp'
11-
})
12-
.done(function(data) {
13-
callback(data);
14-
15-
})
16-
.fail(function(error) {
17-
console.log("Meetup API Request Failed");
1+
/* globals define */
2+
(function(root, factory) {
3+
if (typeof define === 'function' && define.amd) {
4+
// AMD. Register as an anonymous module.
5+
define([], function() {
6+
// Also create a global in case some scripts
7+
// that are loaded still are looking for
8+
// a global even when an AMD loader is in use.
9+
return (root.Meetup = factory());
1810
});
11+
} else {
12+
// Browser globals
13+
root.Meetup = factory();
14+
}
15+
}(this, function() {
16+
'use strict';
17+
18+
// TODO: These URLs should not be hard coded here.
19+
var EVENTS_URL = 'https://api.meetup.com/2/events?offset=0&format=json&' +
20+
'limited_events=False&group_urlname=techcorridorio&page=200&fields=&' +
21+
'order=time&desc=false&status=upcoming&sig_id=168857872&' +
22+
'sig=e659cc6038d27adf6eae600a44905c69196c77df';
23+
24+
var DEVELOPERS_URL = 'https://api.meetup.com/2/members?format=json&' +
25+
'group_urlname=techcorridorio&photo-host=public&order=name&' +
26+
'sig_id=70201382&sig=5b77206251c64989f61e8f45580e0d200221f5d4&' +
27+
'page=20&offset={{offset}}';
28+
29+
function getMeetupEndpoint(url) {
30+
return $.ajax({url: url, dataType: 'jsonp'});
31+
}
32+
33+
function Meetup() {
34+
var requestedPage = window.location.search.match(/page=(\d+)/);
35+
var offset = (requestedPage == null ? 0 : parseInt(requestedPage[1]) - 1);
36+
this.offset = (offset < 0 ? 0 : offset);
37+
this.meetupURL = this._urlFor(EVENTS_URL);
38+
this.developersURL = this._urlFor(DEVELOPERS_URL);
39+
}
40+
41+
Meetup.prototype._urlFor = function(urlTemplate) {
42+
return urlTemplate.replace('{{offset}}', this.offset);
1943
};
2044

21-
var requested_page = window.location.search.match(/page=(\d+)/);
22-
var offset = (requested_page == null ? 0 : parseInt(requested_page[1]) - 1);
23-
offset = (offset < 0 ? 0 : offset);
24-
this.developersURL = "https://api.meetup.com/2/members?format=json&group_urlname=techcorridorio&photo-host=public&order=name&sig_id=70201382&sig=5b77206251c64989f61e8f45580e0d200221f5d4&page=20" +
25-
"&offset=" + offset;
26-
this.getDevelopers = function(callback) {
27-
$.ajax({
28-
url: this.developersURL,
29-
dataType: 'jsonp'
30-
})
31-
.done(function(data) {
32-
callback(data);
33-
})
34-
.fail(function(error) {
35-
console.log("Meetup API Request Failed");
36-
});
45+
Meetup.prototype.getEvents = function() {
46+
return getMeetupEndpoint(this.meetupURL);
47+
};
48+
49+
Meetup.prototype.getDevelopers = function() {
50+
return getMeetupEndpoint(this.developersURL);
3751
};
38-
};
52+
53+
return Meetup;
54+
}));

0 commit comments

Comments
 (0)