Permalink
Browse files

Attempted fix for the endless redirect, also an example to test/demon…

…strate
  • Loading branch information...
1 parent 945c86a commit d1418d3c8e78fe0a39e530fa92bd81cfd6a4def1 @quirkey quirkey committed Jun 24, 2010
Showing with 85 additions and 5 deletions.
  1. +49 −0 examples/back_button/app.html
  2. +15 −0 examples/back_button/index.html
  3. +21 −5 lib/sammy.js
View
49 examples/back_button/app.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+
+ <title>I'm an application!</title>
+
+ <style type="text/css" media="screen">
+
+ </style>
+
+ <script src="../../vendor/jquery-1.4.1.min.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../../lib/sammy.js" type="text/javascript" charset="utf-8"></script>
+ <script type="text/javascript" charset="utf-8">
+ ;(function($) {
+ var app = $.sammy(function() {
+
+ this.get('', function() {
+ return this.redirect('#/');
+ })
+
+ this.get('#/', function() {
+ return this.redirect('#/app')
+ });
+
+ this.get('#/app', function() {
+ $('#main').text('Welcome to my application. Please click back in your browser to return to the index.');
+ });
+
+ });
+
+ $(function() {
+ app.run()
+ });
+ })(jQuery);
+ </script>
+
+</head>
+
+<body>
+ <div id="container">
+ <div id="main">
+ </div>
+ <a href="index.html">Force return to index.</a>
+ </div>
+</body>
+</html>
View
15 examples/back_button/index.html
@@ -0,0 +1,15 @@
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+
+ <title>Welcome!</title>
+
+
+</head>
+
+<body>
+ <div id="container">
+ <a href="app.html">Please go to the application</a>
+ </div>
+</body>
+</html>
View
26 lib/sammy.js
@@ -229,7 +229,7 @@
},
// get the current location from the hash.
getLocation: function() {
- // Bypass the `window.location.hash` attribute. If a question mark
+ // Bypass the `window.location.hash` attribute. If a question mark
// appears in the hash IE6 will strip it and all of the following
// characters from `window.location.hash`.
var matches = window.location.toString().match(/^[^#]*(#.+)$/);
@@ -305,6 +305,7 @@
_last_route: null,
_running: false,
+ _start_url: '',
// Defines what element the application is bound to. Provide a selector
// (parseable by `jQuery()`) and this will be used by `$element()`
@@ -334,6 +335,8 @@
// to if the extension of the partial is null or unknown. See `partial()`
// for more information
template_engine: null,
+
+ history: [],
// //=> Sammy.Application: body
toString: function() {
@@ -573,6 +576,11 @@
return this;
},
+ getHistory: function(index) {
+ if (index < 0) index = this.history.length + index;
+ return this.history[index];
+ },
+
// Takes a single callback that is pushed on to a stack.
// Before any route is run, the callbacks are evaluated in order within
// the current `Sammy.EventContext`
@@ -751,6 +759,7 @@
run: function(start_url) {
if (this.isRunning()) { return false; }
var app = this;
+ this._start_url = start_url || '';
// actually bind all the listeners
$.each(this.listeners.toHash(), function(name, callbacks) {
@@ -1093,6 +1102,7 @@
// reset last location
this.last_location = location;
// lookup route for current hash
+ this.history.push(location);
returned = this.runRoute('get', location);
}
return returned;
@@ -1468,10 +1478,16 @@
to = args[0];
}
this.trigger('redirect', {to: to});
- this.app.last_location = this.path;
- this.app.setLocation(to);
- if (current_location == to) {
- this.app.trigger('location-changed');
+ // check for the possibility of hitting back/stop the endless redirect
+ this.log('path', this.path, 'start_url', this.app._start_url, 'last_location', this.app.last_location, 'to', to, 'history -1', this.app.getHistory(-1));
+ this.log('before history', this.app.history);
+ if (this.app.getHistory(-2) != to) {
+ this.app.last_location = this.path;
+ this.app.setLocation(to);
+ if (current_location == to) {
+ this.app.trigger('location-changed');
+ }
+ this.log('after history', this.app.history);
}
},

0 comments on commit d1418d3

Please sign in to comment.