[Pkg-javascript-commits] [backbone] 100/281: merging in #750 -- support for replaceSTate
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 17:02:00 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.9.0
in repository backbone.
commit 1332246b8f3b6672d3c3f19dd21c99765f4f8282
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Wed Nov 23 14:59:14 2011 -0500
merging in #750 -- support for replaceSTate
---
backbone.js | 45 +++++++++++++++++----------------------------
test/router.js | 6 +++---
test/test.html | 1 -
test/test_helpers.js | 14 --------------
4 files changed, 20 insertions(+), 46 deletions(-)
diff --git a/backbone.js b/backbone.js
index 7cac3b6..ade9db6 100644
--- a/backbone.js
+++ b/backbone.js
@@ -857,36 +857,24 @@
return matched;
},
- // Save a fragment into the hash history, or replace the URL state
- // if the 'replace' option is passed. You are responsible for properly
- // URL-encoding the fragment in advance. This does not trigger
- // a `hashchange` event.
- // parameters:
+ // Save a fragment into the hash history, or replace the URL state if the
+ // 'replace' option is passed. You are responsible for properly URL-encoding
+ // the fragment in advance.
//
- // * fragment: the URL fragment to navigate to (the portion after the '#')
- // * options: An object with the following parameters:
- // - trigger: call the route corresponding to the provided fragment
- // - replace: Navigate such that the back button will
- // not return to this current state.
- //
- // To comply with earlier API specifications, passing
- // true/false for options will be interpretted as
- // {options: trigger: true/false}
+ // The options object can contain `trigger: true` if you wish to have the
+ // route callback be fired (not usually desirable), or `replace: true`, if
+ // you which to modify the current URL without adding an entry to the history.
navigate : function(fragment, options) {
- if (!options || typeof options === 'boolean') options = {trigger: options};
+ if (!options || options === true) options = {trigger: options};
var frag = (fragment || '').replace(hashStrip, '');
if (this.fragment == frag || this.fragment == decodeURIComponent(frag)) return;
if (this._hasPushState) {
if (frag.indexOf(this.options.root) != 0) frag = this.options.root + frag;
this.fragment = frag;
- if (options.replace) {
- window.history.replaceState({}, document.title, frag);
- } else {
- window.history.pushState({}, document.title, frag);
- }
+ window.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, frag);
} else {
this.fragment = frag;
- this._updateLocationHash(window.location, frag, options.replace);
+ this._updateHash(window.location, frag, options.replace);
if (this.iframe && (frag != this.getFragment(this.iframe.location.hash))) {
// Opening and closing the iframe tricks IE7 and earlier to push a history entry on hash-tag change.
// When replace is true, we don't want this.
@@ -897,13 +885,14 @@
if (options.trigger) this.loadUrl(fragment);
},
- // Since you can't run `location.replace` on a hash fragment, this
- // helper function provides an effective work-around.
- _updateLocationHash: function(location, new_fragment, replace) {
- if (replace)
- location.replace(location.toString().replace(/#.*$/, "") + "#" + new_fragment);
- else
- location.hash = new_fragment;
+ // Update the hash location, either replacing the current entry, or adding
+ // a new one to the browser history.
+ _updateHash: function(location, fragment, replace) {
+ if (replace) {
+ location.replace(location.toString().replace(/#.*$/, '') + '#' + fragment);
+ } else {
+ location.hash = fragment;
+ }
}
});
diff --git a/test/router.js b/test/router.js
index 18b6432..88882f9 100644
--- a/test/router.js
+++ b/test/router.js
@@ -94,10 +94,10 @@ $(document).ready(function() {
equals(window.location.hash, "#search/manhattan/finally_here");
window.history.go(-1);
- waitFor(function() { return(window.location.hash != "#search/manhattan/finally_here"); }, 2000).then(function() {
+ setTimeout(function() {
equals(window.location.hash, "#search/manhattan/start_here");
start();
- });
+ }, 500);
});
asyncTest("Router: routes (splats)", function() {
@@ -139,7 +139,7 @@ $(document).ready(function() {
asyncTest("Router: fires event when router doesn't have callback on it", 1, function() {
try{
var callbackFired = false;
- var myCallback = function(){ callbackFired = true; }
+ var myCallback = function(){ callbackFired = true; };
router.bind("route:noCallback", myCallback);
window.location.hash = "noCallback";
setTimeout(function(){
diff --git a/test/test.html b/test/test.html
index 21f94ce..7dda5b3 100644
--- a/test/test.html
+++ b/test/test.html
@@ -9,7 +9,6 @@
<script type="text/javascript" src="vendor/jslitmus.js"></script>
<script type="text/javascript" src="vendor/underscore-1.2.2.js"></script>
<script type="text/javascript" src="../backbone.js"></script>
- <script type="text/javascript" src="test_helpers.js"></script>
<script type="text/javascript" src="noconflict.js"></script>
<script type="text/javascript" src="events.js"></script>
diff --git a/test/test_helpers.js b/test/test_helpers.js
deleted file mode 100644
index f5cb89b..0000000
--- a/test/test_helpers.js
+++ /dev/null
@@ -1,14 +0,0 @@
-function waitFor(state_fn, duration) {
- if(duration === undefined) duration = 1000;
-
- var started_at = new Date;
- var nextActions = [];
-
- var interval_id = setInterval(function() {
- if (((new Date) - started_at > duration) || (state_fn())) {
- clearInterval(interval_id);
- $(nextActions).each(function(_, fn) { fn(); });
- }
- }, 10);
- return({then: function(fn) {nextActions.push(fn);}});
-}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list