-
-
Save harsaharsa/7efcd60c8d75fca130044bd663adff89 to your computer and use it in GitHub Desktop.
Fetch a value via HTTP request and store it to Composer localstorage
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 getScript = function (src, callback) { | |
var el = document.createElement('script'); | |
el.type = 'text/javascript'; | |
el.async = false; | |
el.src = src; | |
/** | |
* Ensures callbacks work on older browsers by continuously | |
* checking the readyState of the request. This is defined once | |
* and reused on subsequeent calls to getScript. | |
* | |
* @param {Element} el script element | |
* @param {Function} callback onload callback | |
*/ | |
getScript.ieCallback = getScript.ieCallback || function (el, callback) { | |
if (el.readyState === 'loaded' || el.readyState === 'complete') { | |
callback(); | |
} else { | |
setTimeout(function () { getScript.ieCallback(el, callback); }, 100); | |
} | |
}; | |
if (typeof callback === 'function') { | |
if (typeof el.addEventListener !== 'undefined') { | |
el.addEventListener('load', callback, false); | |
} else { | |
el.onreadystatechange = function () { | |
el.onreadystatechange = null; | |
getScript.ieCallback(el, callback); | |
}; | |
} | |
} | |
// This is defined once and reused on subsequeent calls to getScript | |
getScript.firstScriptEl = getScript.firstScriptEl || document.getElementsByTagName('script')[0]; | |
getScript.firstScriptEl.parentNode.insertBefore(el, getScript.firstScriptEl); | |
}; | |
getScript('https://code.jquery.com/jquery-2.2.3.min.js', function() { | |
var opts = { | |
type: 'GET', | |
url: 'https://rest-api.appgyver.com/example_api/564080a976aff7bfc5e30042', | |
contentType: false, | |
processData: false, | |
crossDomain: true, | |
success: function(data) { | |
if (data) { | |
supersonic.data.storage.property("fetched-customer").set(data.company); | |
} | |
}, | |
error: function(error) { | |
console.log(error, error.error) | |
} | |
} | |
$.ajax(opts); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment