Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 42a8592

Browse files
committed
allow users to provide state to further normalize the navigation events
1 parent cb636c5 commit 42a8592

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

js/navigation/events/navigate.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,48 @@
33
//>>label: AJAX Navigation System
44
//>>group: Navigation
55

6-
// TODO break out pushstate support test so we don't
7-
// depend on the whole thing
6+
// TODO break out pushstate support test so we don't depend on the whole thing
87
define([ "jquery",
9-
"./../../jquery.mobile.support" ], function( $ ) {
8+
"./../../jquery.mobile.support" ], function( $ ) {
109
//>>excludeEnd("jqmBuildExclude");
1110

1211
(function( $, undefined ) {
13-
var $win = $( window ), self, bound;
12+
var $win = $( window ), self, history;
1413

1514
$.event.special.navigate = self = {
1615
bound: false,
1716

17+
// TODO use the originalEvent property on the event object
18+
// instead of from
1819
popstate: function( event ) {
20+
var state = event.originalEvent.state;
21+
22+
// NOTE the `|| {}` is there to ensure consistency between
23+
// the popstate navigate event and the hashchange navigate
24+
// event data
1925
$win.trigger( new $.Event( "navigate" ), {
2026
from: "popstate",
21-
state: event.originalEvent.state
27+
state: state || {}
2228
});
2329
},
2430

25-
hashchange: function( event ) {
31+
// TODO use the originalEvent property on the event object
32+
// instead of from
33+
hashchange: function( event, data ) {
34+
// Trigger the hashchange with state provided by the user
35+
// that altered the hash
2636
$win.trigger( new $.Event( "navigate" ), {
2737
from: "hashchange",
28-
state: {}
38+
// Users that want to fully normalize the two events
39+
// will need to do history management down the stack and
40+
// add the state to the event before this binding is fired
41+
state: event.hashchangeState || {}
2942
});
3043
},
3144

45+
// TODO We really only want to set this up once
46+
// but I'm not clear if there's a beter way to achieve
47+
// this with the jQuery special event structure
3248
setup: function( data, namespaces ) {
3349
if( self.bound ) {
3450
return;

tests/unit/navigation/event/navigate_core.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,29 @@ $.testHelper.setPushState();
4747
asyncTest( "popstate navigation events contain pushed state", function() {
4848
$( window ).one( "navigate", function( event, data ) {
4949
$( window ).one( "navigate", function( event, data ) {
50-
equal( data.state.foo, "bar", "tagged as popstate" );
50+
equal( data.state.foo, "bar", "state provided properly" );
5151
start();
5252
});
5353

5454
window.history.back();
5555
});
5656

5757
window.history.replaceState({ foo: "bar" }, document.title, location.href.replace(/#.*/, "" ) + "#foo");
58+
location.hash = "#foo2";
59+
});
60+
} else {
61+
// Make sure the binding happends before any of the navigate bindings
62+
$( window ).bind( "hashchange", function( event ) {
63+
event.hashchangeState = { foo: "bar" };
64+
});
65+
66+
asyncTest( "hashchange navigation provides for data added in a later binding", function() {
67+
$( window ).one( "navigate", function( event, data ) {
68+
equal( data.from, "hashchange", "event triggered by a hashchange" );
69+
equal( data.state.foo, "bar", "state provided properly" );
70+
start();
71+
});
72+
5873
location.hash = "#foo2";
5974
});
6075
}

0 commit comments

Comments
 (0)