Coder Social home page Coder Social logo

knockout-router's Introduction

knockout-router

A Knockout.js router using components & templates for pages

Usage

Install:

bower install --save knockout-component-router

Setup:

See knockout-router-example for a detailed demonstration. In short:

  • Create a routes array with each element being an object with the following properties:

    • path (string, required) - The path to trigger the route
    • handler (string|function) - The logic to be run upon route trigger
      • If string (e.g. "userShowHandler"), handler corresponds to a method defined on the router
      • If function, handler will be binded (bound?) to the router
    • page (class|function, required) - The component to be used used/displayed when this route triggers
    • meta (object) - Additional information to be passed to the handlers (i.e. handler, preHandle and postHandle)
      • NB: This provides a really convenient way to dynamically perform different actions within your handlers based on which route was triggered. See the example application for more information
  • Create a SiteRouter which extends Router, optionally overloading the following methods:

    • preHandle - Called before the router handles a path
      • For async stuff, return a Promise
    • postHandle - Called immediately after the router handles a path
    • unrecognizedRouteHandler - Called when the router encounters an unregistered path
      • By default, it throws an Error with the property status = 200, allowing you to catch the error and respond appropriately
  • In your Router's constructor, take in the application view model as a parameter

    • Pass the app and your app's routes to super, i.e. with super(app, routes);
  • Add page handlers to your router by defining methods:

    • E.g. userShowHandler - Called immediately after the preHandle method
    • The return value of the page handlers is passed directly to the component for initialization
      • For async stuff, return a promise
  • In your root application view model, interact with the router like so:

    • Instantiate the router with this.router = new SiteRouter(this);
    • Trigger a route with router.handlePath(path, opts), where path is a string and opts is an optional object
      • opts will be merged with the params generated from the routes and will overwrite keys with the same name

And that's pretty much it!

See the example application for a more detailed and thorough explanation of how to integrate and use the router.

knockout-router's People

Contributors

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