Opened 12 years ago

Closed 12 years ago

#10503 closed defect (invalid)

dojo.hash usage

Reported by: rakeshxp Owned by: Rob Retchless
Priority: high Milestone: tbd
Component: Core Version: 1.4.0
Keywords: Cc:
Blocked By: Blocking:


I tried using the new dojo.hash api in dojo 1.4 and have a query on the basic usage. My current code uses dojo.back code and it works in the following ways:

1) Func1 -> Set hash and adds to history 2) Func2 -> Callback when forward/back button is pressed.

Note that on calling Func1, Func2 is never called as a result of hash change.

But when I tried using dojo.hash in the following way: 1) Func1 -> Sets hash using dojo.hash() 2) dojo.subscribe("/dojo/hashchange", this, function(hash){ console.log(hash); });

In the above case the hashchange event is fired both while setting hash and also when back/forward is set. I was expecting hashchange event to be fired only in case of back/forward. Is this the expected behavior?

Change History (3)

comment:1 Changed 12 years ago by Adam Peller

Owner: changed from anonymous to Rob Retchless

not sure if this is a bug or something better discussed on the mailing lists

comment:2 Changed 12 years ago by Rob Retchless

Copied from email to dojo-interest mailing list:

This is working as expected. If the hash changes, the hashchange event should fire, even if that hash change was initiated by the dojo.hash() setter.

I typically use the following pattern:

1) Subscribe to hashchange topic. 2) User does some action in the UI which either updates the hash directly (<a href="#">) or fires an onclick that calls dojo.hash("someHashValue"). 3) Hashchange event fires, calling your hashchange callback. 4) Update your UI accordingly.

It's important to do your UI updates in the hashchange callback, and not in your onclick, so that the state gets updated properly if the user navigates directly to a hash via a bookmark or copy/paste, etc. If you feel there is too much latency (which is only a concern on IE7, and even then it's a very subtle 100-200ms of latency), you can fire off your UI updates in your onclick, and set a variable so that nothing happens in your hashchange callback if that variable is set. But I don't think that's necessary. It's much cleaner to keep the UI update code separate from the click action code.

You can take a look at for an example of dojo.hash being used in production.

comment:3 Changed 12 years ago by Adam Peller

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.