Last active
December 16, 2015 02:28
-
-
Save ianAndrewClark/5362294 to your computer and use it in GitHub Desktop.
A import•io js client library example by Ian Clark.
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
<html lang="en-us"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Query Example</title> | |
<link rel="stylesheet" href="style.css"> | |
<script src="//d7xe6yl2ckrgs.cloudfront.net/js/2/importio.js"></script> | |
<script src="//d7xe6yl2ckrgs.cloudfront.net/js/0.1/hmac-sha1.js"></script> | |
<script src="//d7xe6yl2ckrgs.cloudfront.net/js/0.1/enc-base64-min.js"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script src="script.js"></script> | |
</head> | |
<body> | |
<div id="search-div"> | |
<form id="search-form" action="javascript:queryModule.execute();"> | |
<input name="postcode" type="text" value="" placeholder="a postcode"> | |
<input type="submit"> | |
</form> | |
</div> | |
<div id="results-div"> | |
<ol id="search-results"> | |
</ol> | |
</div> | |
</body> | |
</html> |
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
var queryModule = (function($, importio, console) { | |
"use strict"; | |
// The query | |
var connectors = { | |
"7874dc2b-f2c2-4527-a2f7-22419399c8c3": "Boots Store Locator", | |
"a9b2f9b7-09e3-487b-a9c1-d54f8c569178": "Argos Store Locator", | |
"e7b6ea68-e52d-4639-bf02-f1952412ae70": "Halifax Store Locator" | |
}; | |
function query(inputValue) { | |
var connectorGuidList = []; | |
for (var property in connectors) connectorGuidList.push(property); | |
return JSON.stringify({ | |
input: { | |
"location/street_address/postal_code/postal_code": inputValue | |
}, | |
maxPages: 5, | |
connectorGuids: connectorGuidList | |
}, null, '\t'); | |
} | |
// Initialisation function | |
function init() { | |
// Init with the User GUID and API Key | |
importio.init({ | |
auth: { | |
userGuid: "7c574c79-0a4e-40af-8a9d-c6234137c230", | |
apiKey: "pu5vIRHiNfxO+yBqNg0BM8pKep9EugFKkgPD8lt+VXv1iGOMiEaBV4iDm3uwTk61GChQQKJfqWEvLsD0Pw7X7w==" | |
} | |
}); | |
console.log("queryModule initialized"); | |
//hook up button click - old school | |
$("#search-form").submit(function() { | |
console.log("executing query"); | |
execute(); | |
return false; | |
}); | |
} | |
// Execute a query | |
function execute() { | |
importio.query(JSON.parse(query($("#search-form input[name='postcode']").val()))) | |
.done(done) | |
.message(message); | |
//clear current results | |
$("#search-results").empty(); | |
} | |
function message(msg) { | |
console.log("Message callback for every CometD message", msg.type, msg); | |
} | |
function done(allData) { | |
console.log("data received"); | |
var resultList = $("#search-results"); | |
//add each result to results list | |
for (var i in allData) { | |
resultList.append('<li>' + connectors[allData[i].connectorGuid] + ' - ' + allData[i].data["location/topic:name"] + ', ' + allData[i].data["location/street_address/street_address"] + '</li>'); | |
} | |
} | |
return { | |
init: init, | |
execute: execute | |
}; | |
})($, importio, window.console); | |
queryModule.init(); | |
//"location/topic:name" | |
//"location/street_address/street_address" |
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
.table tbody tr td.success, | |
.table tbody tr td.success:hover { | |
background-color: #dff0d8; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment