The "Got Statins?" Sample App
The "Got Statins?" App: SMART Connect in Action
If you haven't already, you should read HOWTO Build a SMART App first.
Here, we build a complete SMART Connect app from pure HTML and Javascript. Modeled on the uniquely popular, laser-focused chef-d'oeuvre isitchristmas.com, this app obtains a patient's medication list, determines whether there are statins on board, and relays the answer in large, bold typeface. Using the SMART Connect API, there's no need for explicit authentication, session-management, or AJAX calls.
Every SMART app exposes a URL at index.html where all the fun happens! First,
don't forget to include:
<script src="http://sample-apps.smartplatforms.org/framework/smart/scripts/smart-api-client.js"></script>
Inside index.html, we invoke SMART.ready() with a callback function so we can be sure that the SMART client-side library has finished loading. At this point, a SMART JavaScript object that provides some basic context (such as SMART.user, which provides the name and ID of the user who's running the app, and SMART.record which provides the name and ID of the patient whose record is loaded).
And now we can fetch some data from the patient record:
SMART.get_medications().success(function(meds) { ... });
As you may remember from the HOWTO, meds is a SMART Response object that
includes an RDF graph of medications (type sp:Medication). These medications
include fields such as the
RxNorm concept ID, as well as fulfillment history when available. The whole
RDF graph is wrapped up in an rdfquery
object, providing convenience query functions such as .where(), .optional(),
and .filter() which we can use to interact with the graph.
In this demo app, we use a very low-tech way to figure out of a medication is a station: find its dcterms:title, and string match against the names of all known statins.
var medlist = meds.graph
.where("?m rdf:type sp:Medication")
.where("?m sp:drugName ?n")
.where("?n dcterms:title ?drugname");
Here's how it all comes together:
<!DOCTYPE html>
<html>
<head>
<title>Got Statins?</title>
</head>
<body>
<h1 style="font-family: Arial, sans-serif;">Got Statins?</h1>
<a id="TheAnswer">
...
</a>
<script
src="http://sample-apps.smartplatforms.org/framework/smart/scripts/smart-api-cli
ent.js"></script>
<script>
SMART.ready(function(){
SMART.get_medications().success(function(meds) {
var medlist = meds.graph
.where("?m rdf:type sp:Medication")
.where("?m sp:drugName ?dn")
.where("?dn dcterms:title ?drugname");
var answer = false;
for (var i = 0; i < medlist.length; i++) {
console.log(medlist[i].drugname.value);
if (is_a_statin(medlist[i].drugname.value))
answer = true;
}
document.getElementById("TheAnswer").innerHTML = answer ? "Yes." :
"No.";
});
var is_a_statin = function(drug) {
if (drug.match(/statin/i)) return true;
if (drug.match(/Advicor/i)) return true;
if (drug.match(/Altoprev/i)) return true;
if (drug.match(/Caduet/i)) return true;
if (drug.match(/Crestor/i)) return true;
if (drug.match(/Lescol/i)) return true;
if (drug.match(/Lipitor/i)) return true;
if (drug.match(/Mevacor/i)) return true;
if (drug.match(/Pravachol/i)) return true;
if (drug.match(/Simcor/i)) return true;
if (drug.match(/Vytorin/i)) return true;
if (drug.match(/Zocor/i)) return true;
return false;
}
});
</script>
</body>
</html>
This little app can be statically hosted anywhere. If a SMART container loads it up in an iframe, it will have instant access to the in-context patient record and give us a quick yes or no.