Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow users to provide state to further normalize the navigation events

  • Loading branch information...
commit 42a85927c4582d57d7000e001e147ebc56ca9f66 1 parent cb636c5
@johnbender johnbender authored
View
30 js/navigation/events/navigate.js
@@ -3,32 +3,48 @@
//>>label: AJAX Navigation System
//>>group: Navigation
-// TODO break out pushstate support test so we don't
-// depend on the whole thing
+// TODO break out pushstate support test so we don't depend on the whole thing
define([ "jquery",
- "./../../jquery.mobile.support" ], function( $ ) {
+ "./../../jquery.mobile.support" ], function( $ ) {
//>>excludeEnd("jqmBuildExclude");
(function( $, undefined ) {
- var $win = $( window ), self, bound;
+ var $win = $( window ), self, history;
$.event.special.navigate = self = {
bound: false,
+ // TODO use the originalEvent property on the event object
+ // instead of from
popstate: function( event ) {
+ var state = event.originalEvent.state;
+
+ // NOTE the `|| {}` is there to ensure consistency between
+ // the popstate navigate event and the hashchange navigate
+ // event data
$win.trigger( new $.Event( "navigate" ), {
from: "popstate",
- state: event.originalEvent.state
+ state: state || {}
});
},
- hashchange: function( event ) {
+ // TODO use the originalEvent property on the event object
+ // instead of from
+ hashchange: function( event, data ) {
+ // Trigger the hashchange with state provided by the user
+ // that altered the hash
$win.trigger( new $.Event( "navigate" ), {
from: "hashchange",
- state: {}
+ // Users that want to fully normalize the two events
+ // will need to do history management down the stack and
+ // add the state to the event before this binding is fired
+ state: event.hashchangeState || {}
});
},
+ // TODO We really only want to set this up once
+ // but I'm not clear if there's a beter way to achieve
+ // this with the jQuery special event structure
setup: function( data, namespaces ) {
if( self.bound ) {
return;
View
17 tests/unit/navigation/event/navigate_core.js
@@ -47,7 +47,7 @@ $.testHelper.setPushState();
asyncTest( "popstate navigation events contain pushed state", function() {
$( window ).one( "navigate", function( event, data ) {
$( window ).one( "navigate", function( event, data ) {
- equal( data.state.foo, "bar", "tagged as popstate" );
+ equal( data.state.foo, "bar", "state provided properly" );
start();
});
@@ -57,5 +57,20 @@ $.testHelper.setPushState();
window.history.replaceState({ foo: "bar" }, document.title, location.href.replace(/#.*/, "" ) + "#foo");
location.hash = "#foo2";
});
+ } else {
+ // Make sure the binding happends before any of the navigate bindings
+ $( window ).bind( "hashchange", function( event ) {
+ event.hashchangeState = { foo: "bar" };
+ });
+
+ asyncTest( "hashchange navigation provides for data added in a later binding", function() {
+ $( window ).one( "navigate", function( event, data ) {
+ equal( data.from, "hashchange", "event triggered by a hashchange" );
+ equal( data.state.foo, "bar", "state provided properly" );
+ start();
+ });
+
+ location.hash = "#foo2";
+ });
}
})( jQuery );
Please sign in to comment.
Something went wrong with that request. Please try again.