Giter Site home page Giter Site logo

page-amp's Introduction

Page Amp

Trying to build a reusable router that uses page.js as a routing engine and adds some nice sugar to make it flexible and modular.

I've not tested this at all (not even loaded it in a browser).

This duplicates the backend Controller pattern that we follow at Freightview and expands upon the concept of middleware on the frontend. I'm using this to experiment with loading models outside of the view/router.

Extend the router just like you normally would with Backbone/Ampersand and override setupRoutes:

var Router = require('page-amp'),
    views = require('../views'),
    User = require('../models/user'),
    PostRouter = require('./post');
    

var MainRouter = Router.extend({
  setupRoutes: function(r) {
    r("/", this.loadView('root'));
    r("/login", this.loadView('logIn'));
    r(this.authorize);
    r("/me", this.loadUser, this.loadView('profile'));
    r("/user/:id", this.loadUser, this.loadView('profile'));
    r("/logout", this.logOut, this.redirect('/'));
    
    r.router("/post", PostRouter);
    //this.registerRouter("/post", PostRouter) also works, but I like the r. syntax
  },
  authorize: function(context) {
    //Offload to the server to authenticate.
    //This is mostly for JS apps that don't want to use cookies.
    User.loggedIn({
      success: function(user) {
        context.user = user;
        next();
      },
      error: function(state, err) {
        next(err);
      }
    });
  },
  loadUser: function(context, next) {
    //context is passed to the View when render is called.
    if (context.params.id) {
      context.model = new User({ id: context.params.id || context.user.id });
    }
    context.model.fetch({
      success: function() {
        next();
      },
      error: function(state, err) {
        next(err);
      }
    });
    //You could also do the next() down here and listen for "sync" events in your views.

  },
  logOut: function(context, next) {
    context.model.logOut({
     success: function() {
       next();
     },
     error: function(user, err) {
       next(err);
     }
   });
  }
});

var router = new MainRouter({
  views: views, ///a hash table of views that can be available to this Router, matched via passing the hash key to `loadView`
});

window.app.navigate = router.navigate;
window.app.router = router;

page-amp's People

Contributors

edorsey avatar

Watchers

 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.