Giter Club home page Giter Club logo

backbone-auth's Introduction

Backbone Authorization with Mustache

Managing Roles (Authorization) and content visualization with Backbone and Mustache

Features

  • Visualization of specific elements in Mustache templates according to available Roles;
  • Global access to the Roles through Javacript variables;
  • Request the Roles from an external API;

Requirements

Instalation

  • Script Tag: <script type="text/javascript" src="http://cdn.rawgit.com/Cloudoki/backbone-auth/master/index.js"></script>
  • Bower: bower install git://github.com/Cloudoki/backbone-auth.git
  • npm: npm install github:Cloudoki/backbone-auth

Usage

Initialize the plugin:

  // Define your Roles in an array or json format
  var roles = ['title:view'];             // Roles array
  // var roles = {"title:view": true}     // Roles json

  var auth = new Auth();                  // Initialize the authorizations plugin
  auth.set(roles);                        // Apply the Roles

Run through Mustache:

After initializing the plugin, the Roles will be automatically hooked up on Mustache:

  var template = "{{#_auth_.title:view}} {{title}} {{/_auth_.title:view}} \
                  {{^_auth_.title:view}} Not authorized to see title {{/_auth_.title:view}}";

  this.$el.html(Mustache.render( template, {title: 'Hello World!'}));     // Render

Access Roles on Javascript:

You can also access the Roles through Javascript variables:

  console.log(auth.get('title:view'));    // true
  console.log(auth.get('name:update'));   // undefined

Fetch authorizations from resource API

// You will need to extend the Auth Backbone Model
// to provide the authorizations resource url
var xAuth = Auth.extend({
  url: function(){
    return '/authorizations'
  }
});

var auth = new xAuth();
auth.fetch({
  success: function(){
    console.log('authorizations loaded');
  },
});

Check the examples

You will need a static server

npm install -g http-server

Serve the entire project directory

http-server ./

Open your browser at http://127.0.0.1:8080/examples/

backbone-auth's People

Contributors

efmr avatar muffles avatar a31859 avatar

Watchers

James Cloos avatar Koen Betsens avatar Ricardo Malta avatar  avatar Cátia Araújo avatar Bram Van Oost avatar  avatar Maria Ribeiro avatar

Forkers

tuihackfridays

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.