Created
May 27, 2010 10:27
-
-
Save joeljunstrom/415667 to your computer and use it in GitHub Desktop.
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 $search = $('<input type="search">'), | |
$button = $('<button>Search</button>'), | |
$result_container = $('<div id="app_search_result" />'), | |
$app_list = $('#apps'); | |
var $app_form = $(this).after($search, $button, $result_container).detach(); | |
$search.data('search_context', 'local'); | |
$search | |
.bind('search.apps', function() { | |
$search.trigger('searching.apps'); | |
$.ajax({ | |
url: '/apps/search', | |
data: { | |
'query': $search.val(), | |
'context': $search.data('search_context') | |
}, | |
dataType: 'html', | |
error: function(response, status) { // No hits | |
if (response.status == 404) { | |
if ($search.data('search_context') == 'local') { // first 404? => search the api | |
$search.data('search_context', 'remote'); | |
$search.trigger('search.apps'); | |
} else { // second 404? => display submit new app link | |
$search.trigger('did_not_find.apps'); | |
} | |
} | |
}, | |
success: function(response) { | |
$search.trigger('found.apps', response) | |
} | |
}); | |
}) | |
.bind('searching.apps', function() { | |
if ($search.data('search_context') == 'local') { | |
$result_container.html('Loading...'); | |
} else { | |
$result_container.html('Extreme loading...'); | |
} | |
}) | |
.bind('found.apps', function(event, response) { | |
$result_container.html(response); | |
$('span.add', $result_container).click(addApp); | |
}) | |
.bind('did_not_find.apps', function() { | |
$result_container.html('<p>No matches =(</p>'); | |
}) | |
.bind(($.browser.opera ? "keypress" : "keydown") + ".apps", function(event) { | |
if (event.keyCode == 13) { | |
event.preventDefault(); | |
$search.blur(); | |
$button.trigger('click.apps'); | |
} | |
}); | |
$button.bind('click.apps', function(event) { | |
event.preventDefault(); | |
if ($search.val().length > 0) { | |
$search.trigger('search.apps'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment