Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 3, 2013 07:36
Show Gist options
  • Select an option

  • Save thinkphp/5075209 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/5075209 to your computer and use it in GitHub Desktop.
Instagram Seach using CUI2 AMD Style.
require(["cui","dom","event","ajax"], function(cui, dom, event, ajaxobj){
var sfield = dom.select("#s"),
container = dom.select("#photos"),
timer;
function instaSearch() {
dom.addClass(sfield, "loading")
var q = dom.select("#s").value
var url = "proxy.php?q=" + q;
ajaxobj.ajax("GET", url, function( data ){
//eval JSON received from proxy.php
data = eval("(" + data + ")");
//remove the loading
dom.removeClass(sfield, "loading");
//define an empty variable
var output = '';
//for each object execute
dom.each(data, function(item, i) {
output += '<div class="p"><a href="'+data[i].src+'" class="fullsize" target="_blank"><img src="full-image.png" alt="fullsize"></a> <a href="'+data[i].url+'" target="_blank"><img src="'+data[i].thumb+'"></a></div>';
});
//insert the result to the container
container.html( output );
})
}
/**
* keycode glossary
* 32 = SPACE
* 188 = COMMA
* 189 = DASH
* 190 = PERIOD
* 191 = BACKSLASH
* 13 = ENTER
* 219 = LEFT BRACKET
* 220 = FORWARD SLASH
* 221 = RIGHT BRACKET
*/
event.bind(sfield, "keydown", function( e ){
if(e.keyCode == '32' || e.keyCode == '188' || e.keyCode == '189' || e.keyCode == '13' || e.keyCode == '190' || e.keyCode == '219' || e.keyCode == '221' || e.keyCode == '191' || e.keyCode == '220') {
e.preventDefault();
} else {
clearTimeout(timer);
timer = setTimeout(function() {
instaSearch();
}, 900);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment