Giter Site home page Giter Site logo

jquery-routes's Introduction

jQuery routes - Routing in javascript

Setup

Include the jQuery library and the jquery.routes.js file.

	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
	<script type="text/javascript" src="jquery.routes.js"></script>

IE 9 and below

jQuery routes is now using toISOString function on the Date object. To enable it for older browsers add the toISOString polyfill found on MDN.

IE 7 and below

For legacy browser support, be sure to check out the jQuery.hashchange plugin by Ben Alman. Note that jQuery 1.9 and above is not supported by that plugin.

Usage

Link routes to javascript functions. Make sure the functions prepare the ui for this state of the application. Remember that you can go directly to a route without visiting the main route.

	var newsModule = {
		fetch: function() {
			$('#news').load('news.php?id=' + this.id).show();
		}
		fetchAll: function() {
			$('#news').load('news.php').show();
		}
	};

	$.routes.add('/news/{id:int}/', newsModule.fetch);
	$.routes.add('/news/', newsModule.fetchAll);

or anonymous functions

	$.routes.add('/news/{id:int}/', function() {
		$('#news').load('news.php?id=' + this.id).show();
	});

Parameters are defined with curly braces. The syntax is {name:datatype}. The datatype can be int, float, word, date or you can create your own. Datatypes are added in $.routes.datatypes and parsers in $.routes.parsers. The datatype for parameters can also be a regular expression, ex. {page:news|help|about}. The functions will get named parameters in "this", ex. this.page == 'news'.

Named routes

	// register the route
	$.routes.add('/news/{when:date}/', 'newsByDate', function() {
		alert('loading news from ' + this.when);
	});

	$('#get-news').click(function() {
		// change the url to this route (with this date as parameter)
		$.routes.find('newsByDate').routeTo({
			when: new Date(2010, 1, 19);
		});
	});

Use routeTo function to change the url or use execute function to only execute the function for that route.

Tests

Tests are written using QUnit. Open test/index.html to run the tests.

jquery-routes's People

Contributors

thorsteinsson avatar gislikonrad avatar rthor avatar andref avatar kishan-prolix avatar

Watchers

Anu Prakash avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.