Code Monkey home page Code Monkey logo

lm-router's Introduction

lm-router - simple AngularJS router with real dynamic routing

The idea of this router is to give the ability to have completely dynamic routes without any predefined structure (on the client side). This helps, for example, to build route system which is defined on the server side without the need to expose existing tree to the client, for security or any other purposes. Of course, there can be other usage scenarios as its interface is very generic.

Existing AngularJS routing frameworks require to define the routes on the client side, often without the possibility to override them later.

Usage example

Suppose we have a server backend which has the following interface: when client asks for any URL the server responds with the following:

  • is the url valid: true/false
  • error message in case of any error
  • template to be displayed
  • data to be associated with the template

Then our server can be implemented like this (using Express.js):

var express = require('express');
var app = express();
app.use(express.bodyParser());

app.post('/router', function(req, res) {

    var url = req.body.url;

    if (url === '/test/something') {
        res.send({
            valid: true,
            template: '<p>{{contents}}</p>',
            data: {
                contents: 'abc'
            }
        })
    } else {
        res.send({
            error: 'Unknown URL',
            valid: false
        });
    }
});

app.listen(80);

And our client can look like this:

var app = angular.module('app', ['lm-router']);

app.controller('main', ['$scope', '$parse', '$http', 'router', function($scope, $parse, $http, router) {

    $scope.error = null;
    $scope.body = "<span>Start page</span>";

    $scope.click = function() {
        router.navigate('/test/something');
    };
    
    router.on('(.*)', function(url) {
    
        console.log('Our URL: ' + url);
    
        $http.post('/router', {
            url: url
        }).success(function(response) {
            if (response.valid) {
                $scope.error = null;
                $scope.body = $parse(response.template)(response.data);
            } else {
                $scope.error = response.error;
                $scope.body = "";
            }
        });
    });
    
    router.start();
}]);

angular.bootstrap(document, ['app']);

lm-router's People

Contributors

olegccc avatar

Watchers

 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.