UNPKG

labo-components

Version:
70 lines (64 loc) 2.22 kB
import Resource from '../model/Resource'; const DocumentAPI = { //Primarily called by the ItemDetailsRecipe for fetching all metadata of a single collection item (ES document) getResource :function(collectionId, itemId, collectionConfig, query, callback) { const url = _config.DOCUMENT_API_BASE + '/get_doc/' + collectionId; const xhr = new XMLHttpRequest(); const postData = { id : itemId }; xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if(xhr.status === 200) { callback(Resource.construct(JSON.parse(xhr.responseText), query, collectionConfig)); } else { callback(null); } } }; xhr.open("POST", url); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.send(JSON.stringify(postData)); }, //Primarily called by the ItemDetailsRecipe for fetching all metadata of a single collection item (ES document) getItemDetails :function(collectionId, itemId, callback) { const url = _config.DOCUMENT_API_BASE + '/get_doc/' + collectionId; const xhr = new XMLHttpRequest(); const postData = { id : itemId }; xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if(xhr.status === 200) { callback(collectionId, itemId, JSON.parse(xhr.responseText)); } else { callback(null); } } }; xhr.open("POST", url); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.send(JSON.stringify(postData)); }, //Note: this function is no longer used, but could be again for a new advanced component getItemDetailsMultiple :function(collectionId, idList, callback) { const url = _config.DOCUMENT_API_BASE + '/get_docs/' + collectionId; const xhr = new XMLHttpRequest(); const postData = { ids : idList }; xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if(xhr.status === 200) { callback(collectionId, idList, JSON.parse(xhr.responseText)); } else { callback(null); } } }; xhr.open("POST", url); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.send(JSON.stringify(postData)); } }; export default DocumentAPI;