TralhaController is a class to observe URL changes and notify registered clients. It’s useful to mantain permalinks on webpages that uses ajax.
First we register an observer on TralhaController:
var observer_one = { update: function(url) { // do something, like an ajax request } } TralhaController.addObserver( observer_one );
Say we have link that mark the URL with a #:
<a href="#getinformation">Get Information</a>
When a user clicks on this link, the URL changes:
http://www.example.com/index.html#getinformation
So the object observing the URL is notifed with the new URL on the update method.
If this user sends that URL for a friend, this friend will see the same content because the update method will be called with the same URL.
If you want to make your observers be notified only if the new url has some pattern you can configure on TralhaController it by regular expressions.
Say we have two observers:
var blue_observer = { update: function(url) { // do something, like an ajax request } } var green_observer = { update: function(url) { // do something, like an ajax request } }
And you want that blue_observer be notified when the URLs changed to #blue and green_observers to #green, you can configure like this:
TralhaController.addObserver( "\\#blue", blue_observer ); TralhaController.addObserver( "\\#green", green_observer );
Say you have a video page where you want the user to navigate through a related videos box. It is paginated and you don’t want it to reload the page and make the user reload the video that he was watching.
Also, you want to be able to copy the URL of the video with the page 3 on that box loaded and you want that your user be able to share it with his friend to see the page 3 and the video (all the context shared).
With this script you can do that. You can watch changes after # and put there contextual information. When you send the url to a friend and he/she loads it, the observer will read that and load the third page of your related videos box.
There are no known issues at the time.
- Fixed
-
When you add more than one observers, only the first is notified when page is first loaded.
This class was tested and works on the following browsers:
* Internet Explorer 6 * Internet Explorer 7 * Firefox 2 * Firefox 3 * Opera 9.4