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

Commit

Permalink
allow users to provide state to further normalize the navigation events
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbender committed Oct 10, 2012
1 parent cb636c5 commit 42a8592
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
30 changes: 23 additions & 7 deletions js/navigation/events/navigate.js
Expand Up @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/navigation/event/navigate_core.js
Expand Up @@ -47,14 +47,29 @@ $.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();
});

window.history.back();
});

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";
});
}
Expand Down

0 comments on commit 42a8592

Please sign in to comment.