Skip to content

timotta/TralhaController

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

TralhaController

TralhaController is a class to observe URL changes and notify registered clients. It’s useful to mantain permalinks on webpages that uses ajax.

Example

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.

Observing an url pattern

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 );

Possible use scenario

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.

Known Issues

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

About

A javascript class to observe URL changes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published