GithubHelp home page GithubHelp logo

teng2015 / ko-component-router Goto Github PK

View Code? Open in Web Editor NEW

This project forked from profiscience/ko-component-router

0.0 1.0 0.0 14.45 MB

:station: Component based router for developing wicked awesome apps with Knockout

License: Do What The F*ck You Want To Public License

JavaScript 62.01% TypeScript 37.99%

ko-component-router's Introduction

ko-component-router

NPM Version WTFPL Travis Coverage Status Dependency Status Peer Dependency Status NPM Downloads Gitter

Super-duper flexible component based router for developing wicked awesome single page apps with KnockoutJS

YOU ARE ON THE next BRANCH

oh hai, a blog post on changes in ko-component-router@next, fancy that.

Installation

$ yarn add ko-component-router@next

...or...

$ npm install -S ko-component-router@next

Usage

app.js

import $ from 'jquery'
import ko from 'knockout'
import Router from 'ko-component-router'

const loading = ko.observable(true)

Router.use(loadingMiddleware)

Router.useRoutes({
  '/': 'home',
  '/users': {
    '/': [loadUsers, 'users'],
    '/:id': [loadUser, 'user']
  }
})

ko.component.register('home', {
  template: `<a data-bind="path: '/users'">Show users</a>`
})

ko.components.register('users', {
  viewModel: class UsersViewModel {
    constructor(ctx) {
      this.users = ctx.users
    }

    navigateToUser(user) {
      Router.update('/users/' + user.id, { with: { user } })
    }
  },
  template: `
    <ul data-bind="foreach: users">
      <span data-bind="text: name, click: navigateToUser"></span>
    </ul>
  `
})

ko.components.register('user', {
  viewModel: class UserViewModel {
    constructor(ctx) {
      this.user = ctx.user
    }
  },
  template: `...`
})

function loadingMiddleware(ctx) {
  return {
    beforeRender() {
      loading(true)
    },
    afterRender() {
      loading(false)
    }
  }
}

// generators are also supported if you're a pioneer of sorts
// function * loadingMiddleware(ctx) {
//   loading(true)
//   yield
//   loading(false)
// }

function loadUsers(ctx) {
  // return promise for async middleware
  return $.get('/api/users/').then((us) => ctx.users = us)
}

function loadUser(ctx) {
  // if not passed in via `with` from Users.navigateToUser
  if (!ctx.user) {
    return $.get('/api/users/' + ctx.params.id).then((u) => ctx.user = u)
  }
}

ko.applyBindings({ loading })

index.html

<ko-component-router data-bind="css: { opacity: loading() ? .5 : 1 }"></ko-component-router>

More

ko-component-router's People

Contributors

barake avatar barsh avatar carlosagsmendes avatar caseywebb avatar cbclemmer avatar gitter-badger avatar greenkeeper[bot] avatar greenkeeperio-bot avatar matt-hensley avatar meltuhamy avatar xtr0 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.