SiZapPaaiGwat / inhuman-cpc.github.io

我的博客
431 stars 94 forks source link

前端利器之路由库 #3

Open SiZapPaaiGwat opened 9 years ago

SiZapPaaiGwat commented 9 years ago

前端利器之路由库

Crossroads.js

Crossroads.js is a routing library inspired by URL Route/Dispatch utilities present on frameworks like Rails, Pyramid, Django, CakePHP, CodeIgniter, etc... It parses a string input and decides which action should be executed by matching the string against multiple patterns.

It is a powerful and flexible routing system. If used properly it can reduce code complexity by decoupling objects and also by abstracting navigation paths and server requests.

Examples

Finch.js

Finch.js is a whole new take on handling routes in javascript web apps. It utilizes the natural hierarchy of routes, simplified pattern matching, and powerful parameter dependencies in order to speed up and organize web apps that rely highly on interecting with the browser's url.

page.js

Micro client-side router inspired by the Express router.

Sammy

Sammy is a tiny javascript framework built on top of jQuery inspired by Ruby's Sinatra.

PathJS

PathJS is a lightweight, client-side routing library that allows you to create "single page" applications using Hashbangs and/or HTML5 pushState.

Director

Director is a router. Routing is the process of determining what code to run when a URL is requested.

SiZapPaaiGwat commented 9 years ago

于是写了个Crossroads.js 的demo,发现了其中的一些主要特点:

  • API接口设计良好,功能强大
  • 已经非常稳定,在论坛、社区很多牛人推荐过。
  • github两年没有更新代码,太稳定了?
  • 依赖较多 ( hasher.js 和signals),和作者追求单一职责原则有关

关于依赖问题,其实我也赞同Crossroads这样的设计,但使用起来实在不顺手。 另外各个组件在接口层面的一致性有问题。比如在设置url的功能上,url是否以/开始会导致一些小问题。 虽然不是什么大问题,但是十分诡异,算得上一个坑。

demo地址

SiZapPaaiGwat commented 9 years ago

经过最终考虑还是选择了director.js,不过只选择了browser部分

  • 更新频繁,看起来更有生命力
  • 调用简单,使用更顺手
  • route本身的实现并不复杂