Permalink
Browse files

change to use director instead of jquery.dispatch.js and improve live…

… search handling
  • Loading branch information...
1 parent 674578c commit bc9036a360fc815b686a52c741014a9dfb79eec6 @caolan committed Jun 15, 2012
Showing with 67 additions and 39 deletions.
  1. +5 −0 js/director.min.js
  2. +0 −12 js/jquery.dispatch.min.js
  3. +12 −8 ui/app.js
  4. +50 −19 ui/views/home.js
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,12 +0,0 @@
-/**
- * jquery.dispatch.js
- * ==================
- *
- * A lightweight jQuery plugin for client-side URL handling. Dispatches
- * hash-based URLs to view functions, supports nested pattern objects,
- * regular expressions and :named parameters.
- *
- * Caolan McMahon
- * http://caolanmcmahon.com
- *
- */(function(a){function b(a,c,d,e){return c=c||[],d=d||"",Object.keys(a).forEach(function(e){typeof a[e]=="function"?c.push([d+e,a[e]]):b(a[e],c,d+e)}),c}function c(a){return a.map(function(a){var b=a[0].replace(/\/:\w+/g,"(?:/([^/]+))");return a[0]=new RegExp("^"+b+"$"),a})}a.dispatch=function(d){var e=c(b(d)),f=function(){for(var a=0,b=e.length;a<b;a++){var c=e[a],d=c[0].exec(window.location.hash.substr(1));if(d){c[1].apply(null,d.slice(1));return}}};a(window).bind("hashchange",f)}})(window.jQuery);
View
@@ -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');
+ }
};
View
@@ -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');
@@ -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');

0 comments on commit bc9036a

Please sign in to comment.