Created
June 26, 2014 00:15
-
-
Save alanmclean/e9d14d94a2aa2cc2cc24 to your computer and use it in GitHub Desktop.
for strava stream requests, assuming x/y mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var strava = strava || {}; | |
// https://www.strava.com/api/v3/activities/131057926/streams/heartrate?access_token=6d8df9236d33c0e00ad517e720f9cd3c6d58e265 | |
strava.streamLoader = function(streams){ | |
_.extend(this, Backbone.Events); | |
this.streams = streams; | |
this.x = streams.x; | |
this.y = streams.y; | |
this.activity = streams.activity; | |
this.accessToken = '6d8df9236d33c0e00ad517e720f9cd3c6d58e265'; | |
this.url = 'http://www.strava.com/api/v3/activities/'+this.activity+'/streams/'+this.x+','+this.y+'?access_token='+this.accessToken+'&callback=?'; | |
}; | |
strava.streamLoader.prototype = { | |
request: function(){ | |
var me = this; | |
jQuery.ajax({ | |
url: me.url, | |
dataType: 'jsonp', | |
type: 'GET', | |
success: _.bind(me.onSuccess, me), | |
error: _.bind(me.onError, me) | |
}); | |
}, | |
formatData: function(data){ | |
var me = this, yData, xData; | |
data.forEach(function(d){ | |
if (d.type == me.y){ | |
yData = d.data; | |
}else if (d.type == me.x){ | |
xData = d.data; | |
} | |
}); | |
data = _.map(xData , function(d,i){ | |
var h = {}; | |
h[me.x] = d; | |
h[me.y] = yData[i]; | |
return h; | |
}); | |
return data; | |
}, | |
onSuccess: function(data){ | |
this.data = this.formatData(data); | |
this.trigger('ready', { activity: this.activity, data: this.data }); | |
}, | |
onError: function(data){ | |
this.trigger('error', data) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment