From 898a18df9a0938041e58f92930443a3c88b78f27 Mon Sep 17 00:00:00 2001 From: Devin Weaver Date: Thu, 5 Nov 2015 19:53:43 -0500 Subject: [PATCH] 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. --- js/meetup-events.js | 84 +++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 34 deletions(-) diff --git a/js/meetup-events.js b/js/meetup-events.js index 2e67cbc..a37c9f7 100644 --- a/js/meetup-events.js +++ b/js/meetup-events.js @@ -1,38 +1,54 @@ -'use strict'; - -var Meetup = function(meetupURL) { - this.meetupURL = (typeof meetupURL!=='undefined') ? meetupURL : - "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"; - - this.getEvents = function(callback) { - $.ajax({ - url: this.meetupURL, - dataType: 'jsonp' - }) - .done(function(data) { - callback(data); - - }) - .fail(function(error) { - console.log("Meetup API Request Failed"); +/* globals define */ +(function(root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define([], function() { + // Also create a global in case some scripts + // that are loaded still are looking for + // a global even when an AMD loader is in use. + return (root.Meetup = factory()); }); + } else { + // Browser globals + root.Meetup = factory(); + } +}(this, function() { + 'use strict'; + + // TODO: These URLs should not be hard coded here. + var EVENTS_URL = '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'; + + var DEVELOPERS_URL = 'https://api.meetup.com/2/members?format=json&' + + 'group_urlname=techcorridorio&photo-host=public&order=name&' + + 'sig_id=70201382&sig=5b77206251c64989f61e8f45580e0d200221f5d4&' + + 'page=20&offset={{offset}}'; + + function getMeetupEndpoint(url) { + return $.ajax({url: url, dataType: 'jsonp'}); + } + + function Meetup() { + var requestedPage = window.location.search.match(/page=(\d+)/); + var offset = (requestedPage == null ? 0 : parseInt(requestedPage[1]) - 1); + this.offset = (offset < 0 ? 0 : offset); + this.meetupURL = this._urlFor(EVENTS_URL); + this.developersURL = this._urlFor(DEVELOPERS_URL); + } + + Meetup.prototype._urlFor = function(urlTemplate) { + return urlTemplate.replace('{{offset}}', this.offset); }; - var requested_page = window.location.search.match(/page=(\d+)/); - var offset = (requested_page == null ? 0 : parseInt(requested_page[1]) - 1); - offset = (offset < 0 ? 0 : offset); - 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" + - "&offset=" + offset; - this.getDevelopers = function(callback) { - $.ajax({ - url: this.developersURL, - dataType: 'jsonp' - }) - .done(function(data) { - callback(data); - }) - .fail(function(error) { - console.log("Meetup API Request Failed"); - }); + Meetup.prototype.getEvents = function() { + return getMeetupEndpoint(this.meetupURL); + }; + + Meetup.prototype.getDevelopers = function() { + return getMeetupEndpoint(this.developersURL); }; -}; + + return Meetup; +}));