Skip to content

Commit

Permalink
+ back/forward works, correct regexp for extracting the query part, i…
Browse files Browse the repository at this point in the history
…nserting works
  • Loading branch information
floere committed Mar 14, 2011
1 parent c9511e1 commit e209895
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 58 deletions.
34 changes: 17 additions & 17 deletions generators/client_test/javascripts/picky.min.js

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions generators/javascripts/picky.client.js
Expand Up @@ -62,7 +62,7 @@ var PickyClient = function(config) {
};
var insert = this.insert;

// Takes a #/?q=example parameter
// Takes a query or nothing as parameter.
//
// And runs a query with it (if $.address exists).
// Can be overridden with a non-empty parameter.
Expand All @@ -71,15 +71,17 @@ var PickyClient = function(config) {
if (override && override != '') {
insert(override);
} else {
$.address && insert($.address.parameter('q'));
var lastQuery = controller.lastQuery();
lastQuery && insert(lastQuery);
}
};

// Adapter to let the back/forward button start queries.
// Bind adapter to let the back/forward button start queries.
//
if ($.address) {
$.address.externalChange(function(event) {
var query = event.parameters['q'];
if (window.History) {
window.History.Adapter.bind(window, 'statechange', function() {
var state = window.History.getState();
var query = controller.extractQuery(state.url);
query && insert(query);
});
};
Expand Down
24 changes: 23 additions & 1 deletion generators/javascripts/picky.controller.js
Expand Up @@ -7,6 +7,23 @@ var PickyController = function(config) {
var successCallback = config.success || function(data, query) { };
var afterCallback = config.after || function(data, query) { };

// Extracts the query part from an URL.
//
var extractQuery = function(url) {
var match = url && url.match(/q=([^&]+)/);
return match && match[1];
};
this.extractQuery = extractQuery;

// Very failsafe extraction of the last made query.
//
var lastQuery = function() {
var state = window.History && window.History.getState();
var url = state && state.url;
return extractQuery(url);
};
this.lastQuery = lastQuery;

// If the given backend cannot be found, ignore the search request.
//
var search = function(type, query, callback, offset, specificParams) {
Expand Down Expand Up @@ -54,7 +71,12 @@ var PickyController = function(config) {

// Be extra cautious since not all browsers/histories offer pushState.
//
window.History && window.History.pushState && window.History.pushState(null, null, "?q=" + query);
// Note: If this query is the same as the last, we do not save it in the history.
//
if (query != lastQuery()) {
var url = "?q=" + query;
window.History && window.History.getState() && window.History.pushState && window.History.pushState(null, null, url);
}

params = beforeCallback(params, query, offset) || params;

Expand Down

0 comments on commit e209895

Please sign in to comment.