Giter Site home page Giter Site logo

partial.js-clientside's Introduction

partial.js client-side routing

Framework supports the HTML 5 History API, for older browsers (IE8+) URL hashtag is automatically enabled. This plugin is a little big cannon for the web development. Works best with jQuery.

  • easy to use
  • minified only 9.5 kB (without GZIP compression)
  • great functionality
  • great use
  • works in IE 8+
  • no dependencies
  • DEMO EXAMPLE

MUST SEE:

Simple example

// ===========================
// DEFINE ROUTING
// ===========================

// framework === global variable

framework.route('/homepage/', view_homepage, ['contact']);
framework.route('/services/', view_services, ['contact']);
framework.route('/contact/', view_contact, ['empty']);
framework.route('/products/{category}/', view_products, ['latest']);

// ===========================
// DEFINE PARTIAL CONTENT
// ===========================

framework.partial('contact', function() {
    $('#panel').html('PANEL CONTACT');
});

framework.partial('empty', function() {
    $('#panel').html('PANEL EMPTY');
});

framework.partial('latest', function() {
    $('#panel').html('PANEL LATEST');
});

// ===========================
// DEFINE VIEWS
// ===========================

function view_homepage() {
    $('#content').html('HOMEPAGE');
}

function view_services() {
    $('#content').html('SERVICES');
}

function view_contact() {
    $('#content').html('CONTACT');
}

function view_products(category) {
	$('#content').html('PRODUCTS –> ' + category);
}

// ===========================
// DEFINE EVENTS
// ===========================

framework.on('ready', function() {
    $('.menu').on('click', 'a', function(e) {
        e.preventDefault();
        e.stopPropagation();
        framework.redirect($(this).attr('href'));
    });
    framework.redirect('/homepage/');
});

framework.on('location', function(url) {
     var menu = $('.menu');
     menu.find('.selected').removeClass('selected');
     menu.find('a[href="' + url + '"]').parent().addClass('selected');
});

Properties

framework.isModernBrowser;

{Boolean} - Supports browser HistoryAPI?

framework.url;

{String} - Current URL address.

console.log(framework.url);

framework.version;

{Number} - Current framework version.

console.log(framework.version);

framework.history;

{String Array} - History list (LIMIT_HISTORY === 100).

console.log(framework.history);

framework.errors;

{String Array} - Error list (LIMIT_HISTORY_ERROR === 100).

console.log(framework.errors);

framework.global;

{Empty object} - Temporary global object for storing a temporary data.

framework.global.secret = 'AbcaDUIAZ349';
framework.global.name = 'partial.js';

framework.repository;

{Empty Object} - Temporary object for the current location. After redirecting is the repository object cleared.

framework.repository.title = 'partial.js title';

framework.model;

{Object} - model for the current location.

framework.redirect('/new-url/', { name: 'partial.js '});

// --> view

function view_new_url() {
	// this === framework
	var self = this;
	console.log(self.model);
}

framework.isReady;

{Boolean} - Is framework ready??

console.log(framework.isReady);

framework.isRefresh;

{Boolean} - Is refresh?

function view() {
	var self = this;
	// --> self.refresh();
	console.log(self.isRefresh);
}

framework.get;

{Object} - Current (GET) params from URL address (url -> query). After redirect or refresh are params re-loaded.

// ---> /current-page/?q=partial.js
console.log(framework.get.q);

## Methods

framework.route(path, fn, [partials], [once])

Create a route.

framework.route('/', view_homepage);
framework.route('/products/{category}/', view_products, ['latest']);

framework.partial(name, fn)

Create a partial content

framework.partial('latest', function() {
	console.log('latest products');
});

framework.redirect(url, [model])

Redirect.

framework.redirect('/products/shoes/');

// or

framework.redirect('/products/shoes/', { from: 'jeans', latest: true, custom: 'model' });

framework.back()

History back.

framework.back();

framework.refresh()

Refresh current page.

framework.refresh();

Events

framework.on('ready')

Is framework ready?

framework.once('ready', funtion() {
	console.log('I\'m ready');
	framework.redirect('/homepage/');
});

framework.on('location')

Capture a new location.

framework.on('location', function(url) {
	console.log('new location --->', url);
});

framework.on('error')

Capture an error.

framework.on('error', function(error, url, description) {
	console.log('ERROR --->', error, url, description);
});

framework.on('status')

Capture an HTTP status.

framework.on('status', function(code, message) {

	switch (code) {
		case 404:
			console.log('NOT FOUND', message);
			break;
		case 500:
			console.log('INTERNAL ERROR', message);
			break;
	}

});

partial.js-clientside's People

Contributors

petersirka avatar michalstanko avatar

Watchers

saksoy avatar James Cloos 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.