Giter Site home page Giter Site logo

flight-router's Introduction

Simple Router mixin-component for twitter flight

You need to mixit it up with one of your components and you will be able to use API to listen routes and and move location to needed path.

Usage example:

####Component with router

define(['path/to/flight-component','path/to/flight-router'], function(defineComponent, router) {
    return defineComponent(newComponent, router);

    function newComponent() {
        this.index = function() {
            alert('INDEX');
        };
        this.basicHelp = function() {
            alert('HELP');
        };
        this.helpWithParams = function(arg) {
            alert(JSON.stringify(arg));
        };
        this.after('initialize', function() {
            this.defineRoute({
                '/' : 'index', //Simple root listener
                '/help' : 'basicHelp', // Simple URL listener
                '/help/:article/:page' : 'helpWithParams', // Parametrized URL listener
                '/:pageType/:article/:page' : 'helpWithParams', // Will match any url like "/asd/wtf/123"
                '/help?:param=:value&:param2=:value2' : 'helpWithParams' // Will match get params
            });
            var counter = 0;
            var index = [
                '/help',
                '/help/how-to-create-route/page2',
                '/FAQ/navigate/1',
                '/help?get=something&andMore=something:dotted'
            ]
            this.on('click', function() {
		if (counter > index.length) {
		    counter = 0;
		}
	        this.navigate(index[counter]);
                counter++;
            });
        });
    }
});

####Initializing

require(['path/to/flight-component-with-router'], function(component) {
    document.body.innerHTML = 'Click on body to change url and trigger handler \n \
    Try to press BACK and FORWARD button and your listeners will fire';
    component.attachTo(document.body);
});

Documentation

this.defineRoute(route, callback);

Route is URL like string, where :variable_like_this will be variable. Callback must be a string or function. If string provided callback would be taken from 'this' by this string. Every callback will be fired with 'this' component context.

this.defineRoute(routesHash);

RoutesHash is hash with route / callback key-value pairs. Like: this.defineRoute({'/', 'callback'});

this.navigate(URL, [args]);

URL is url like string. Must start from '/'. Args is optional object with following params:

    replaceState: Boolean

If passed state would not be pushed, but replaced, to save history clean.

    data: Object

Data that would be passed to handlers AND state.

    forced: Boolean

Param that tells fire new state any way even if URL is same.

####Use it

flight-router's People

Contributors

sergeystoma avatar

Watchers

 avatar  avatar  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.