Skip to content

Instantly share code, notes, and snippets.

@chrissnyder
Created July 10, 2015 01:48
Show Gist options
  • Save chrissnyder/a139c27a939f2fb8a09a to your computer and use it in GitHub Desktop.
Save chrissnyder/a139c27a939f2fb8a09a to your computer and use it in GitHub Desktop.
Project = Backbone.Model.extend({
urlRoot: "https://panoptes-staging.zooniverse.org/api/subject_sets?project_id="+window.project_id,
// Specify default values for the model.
defaults: function(){
return {
slug: 'default_slug',
display_name: '',
introduction: '',
education_content: '',
project_avatar: '',
workflow_description: ''
}
},
idAttribute: 'slug'
});
/* General-purpose model for a collection of SubjectSets */
Projects = Backbone.Collection.extend({
model: Project,
sync: function(method, model, options) {
var params = _.extend({
type: 'GET',
dataType: 'application/json',
url: model.url(),
processData: false
}, options);
return $.ajax(params);
},
parse: function(response) {
// Iterate over every project received and to update the project_avatar url
for(var index in response.projects) {
// Get the ID of the Avatar
var avatar_id = response.projects[index].links.avatar.id;
// Make the request for the avatar
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://panoptes-staging.zooniverse.org/api/projects/" + window.project_id + "/avatar?id=" + avatar_id, false);
xhr.setRequestHeader('Accept', "application/vnd.api+json; version=1");
xhr.setRequestHeader('Content-Type', "application/json");
xhr.send();
// Parse into JSON object
var jResponse = JSON.parse(xhr.responseText);
// Change the project_avatar attribute for the project
response.projects[index]['project_avatar'] = jResponse.media[0].src;
}
console.log(response.projects);
// Return the updated projects.
return response.projects;
},
url: function() {
// Return Collections with specific ID
//return "https://panoptes-staging.zooniverse.org/api/subject_sets?project_id="+window.project_id;
// Return a single project.
return "https://panoptes-staging.zooniverse.org/api/projects?id="+window.project_id;
// Return all projects tagged as Ancient Lives projects.
//return "https://panoptes.zooniverse.org/api/projects?tags=Ancient+Lives"
}
});
module.exports = {
model: Project,
collection: Projects
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment