Skip to content

Commit

Permalink
change to use director instead of jquery.dispatch.js and improve live…
Browse files Browse the repository at this point in the history
… search handling
  • Loading branch information
Caolan McMahon committed Jun 15, 2012
1 parent 674578c commit bc9036a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 39 deletions.
5 changes: 5 additions & 0 deletions js/director.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions js/jquery.dispatch.min.js

This file was deleted.

20 changes: 12 additions & 8 deletions ui/app.js
@@ -1,11 +1,15 @@
exports.init = function (url) {
$.dispatch({
'': require('./views/home'),
'/': require('./views/home'),
'/signup': require('./views/signup'),
'/login': require('./views/login'),
'/login/:next': require('./views/login'),
'/profile/:id': require('./views/profile')
var router = Router({
'/': require('./views/home'),
'/search/*': require('./views/home'),
'/signup': require('./views/signup'),
'/login': require('./views/login'),
'/login/*': require('./views/login'),
'/profile/*': require('./views/profile')
});
$(window).trigger('hashchange');
router.init();
if (!window.location.hash || window.location.hash === '#') {
window.location = '#/';
$(window).trigger('hashchange');
}
};
69 changes: 50 additions & 19 deletions ui/views/home.js
Expand Up @@ -4,32 +4,58 @@ var templates = require('handlebars').templates,
cfg = require('settings/root');


// max number of profiles to list on page
var PAGE_LENGTH = 5;
var PAGE_LENGTH = 5, // max number of profiles to list on page
prev_search = null; // to stop queries for the same results

module.exports = function () {
$('#content').html(
templates['home.html']({})
);
if (cfg.profiles && cfg.profiles.lucene) {
$('#user-search-form').show();
$('#user-search-form').submit(function (ev) {
ev.preventDefault();
var q = $('#user-search-q', this).val();
searchProfiles(q);
return false;
});$
$('#user-search-q').keyup($.debounce(500, function () {
var q = $(this).val();
// set to true when updating the page inline but changing the url
// for back button / bookmarking support
var ignore_next_hashchange = false;


module.exports = function (q) {
if (ignore_next_hashchange) {
// already showing home page, it will have updated inline
// without redrawing. do nothing on hash change.
ignore_next_hashchange = false;
}
else {
$('#content').html(
templates['home.html']({})
);
if (cfg.profiles && cfg.profiles.lucene) {
$('#user-search-form').show();
$('#user-search-form').submit(function (ev) {
ev.preventDefault();
var q = $('#user-search-q', this).val();
searchProfiles(q);
return false;
});$
$('#user-search-q').keyup($.debounce(500, function () {
var q = $(this).val();
if (q !== prev_search) {
searchProfiles(q);
}
prev_search = q;
}));
$('#user-search-q').focus();
}
if (q) {
var q = decodeURIComponent(q);
$('#user-search-q').val(q);
searchProfiles(q);
}));
$('#user-search-q').focus();
}
else {
getProfiles();
}
}
getProfiles();
};


function getProfiles(start_key, descending) {
if (window.location.hash !== '#/') {
ignore_next_hashchange = true;
window.location = '#/';
}
$('#user-search-form .control-group').removeClass('error');
$('#user-search-form .help-inline').text('');
$('#user-search-submit').button('loading');
Expand Down Expand Up @@ -60,6 +86,11 @@ function searchProfiles(q, skip) {
if (!q) {
return getProfiles();
}
var newurl = '#/search/' + encodeURIComponent(q);
if (window.location.hash !== newurl) {
ignore_next_hashchange = true;
window.location = newurl;
}
$('#user-search-form .control-group').removeClass('error');
$('#user-search-form .help-inline').text('');
$('#user-search-submit').button('loading');
Expand Down

0 comments on commit bc9036a

Please sign in to comment.