Created
July 13, 2015 20:06
-
-
Save acwio/d4eae90385edb0cc52c2 to your computer and use it in GitHub Desktop.
API in the Model.
This file contains hidden or 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
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: '', | |
description: '', | |
education: '', | |
project_avatar: '', | |
workflow_description: '', | |
result: '', | |
faq: '', | |
science_case: '', | |
background_image: '', | |
percent_complete: '0' | |
} | |
}, | |
idAttribute: 'slug', | |
fetchAvatar: function(){ | |
console.log(this); | |
/* get the model's avatar id attribute */ | |
var avatar_id = this.get("links").avatar.id; | |
/* make the call to retrieve the avatar via api */ | |
var that = this; | |
window.apiClient.api.type('projects').get(window.project_id+'/avatar/?id='+avatar_id).then(function(av_response) { console.log(av_response); that.set({'project_avatar': av_response.src}) }); | |
} | |
}); | |
/* 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) { | |
// Return the updated projects. | |
console.log(response.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" | |
}, | |
fetchAdditional: function() { | |
/* update model attributes via api */ | |
var that = this; | |
window.apiClient.api.type('projects').get(window.project_id).then( | |
function(project) { | |
/* retrieve all the pages for the project */ | |
project[0].get('pages').then( | |
function(pages) { | |
/* iterate through each page | |
* and add content to the model */ | |
for(var key in pages){ | |
/* check for presence of 'url_key' */ | |
if('url_key' in pages[key]) { | |
console.log(pages[key].url_key); | |
/* add the relevant content */ | |
if (pages[key].url_key == "education") { | |
that.set({'education': pages[key].content}); | |
} | |
else if (pages[key].url_key == "faq") { | |
that.set({'faq': pages[key].content}); | |
} | |
else if (pages[key].url_key == "results") { | |
that.set({'results': pages[key].content}); | |
} | |
else if (pages[key].url_key == "science_case") { | |
that.set({'science_case': pages[key].content}); | |
} | |
} | |
} | |
}) | |
}); | |
/* update model background image via api */ | |
// TODO: Validate "header.png" | |
window.apiClient.api.type('projects').get(window.project_id+'/attached_images').then(function(response) { that.set({'background_image': response.src});}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment