Permalink
Browse files

make it work on the server, too

  • Loading branch information...
1 parent d63e725 commit 6d71b2de3cc3fc75edf232999efa92be80802053 @amccollum committed Apr 22, 2012
Showing with 102 additions and 139 deletions.
  1. +0 −3 Cakefile
  2. +3 −5 lib/ender.js
  3. +52 −0 lib/index.js
  4. +0 −61 lib/route.js
  5. +1 −1 package.json
  6. +4 −3 src/ender.coffee
  7. +42 −0 src/index.coffee
  8. +0 −66 src/route.coffee
View
@@ -1,9 +1,6 @@
fs = require('fs')
-sys = require('sys')
{spawn, exec} = require('child_process')
-package = JSON.parse(fs.readFileSync('package.json', 'utf8'))
-
execCmds = (cmds) ->
exec cmds.join(' && '), (err, stdout, stderr) ->
output = (stdout + stderr).trim()
View
Oops, something went wrong.
View
Oops, something went wrong.
View
@@ -1,61 +0,0 @@
-(function(route) {
- var Route, _hash, _routes;
- _routes = [];
- _hash = null;
- route.init = function(run) {
- var onchange;
- onchange = function() {
- var hash;
- hash = $.hash();
- if (hash !== _hash) {
- _hash = hash;
- route.run(hash);
- }
- };
- $(window).bind('hashchange', onchange);
- if (run) {
- onchange();
- }
- };
- route.navigate = function(hash, run) {
- if (!run) {
- _hash = hash;
- }
- $.hash(hash);
- };
- route.run = function(hash) {
- var m, route, _i, _len;
- for (_i = 0, _len = _routes.length; _i < _len; _i++) {
- route = _routes[_i];
- if ((m = route.pattern.exec(hash))) {
- route.fn.apply(route, m.slice(1));
- }
- }
- };
- route.add = function(routes, fn) {
- var path;
- if (fn) {
- routes = {};
- routes[routes] = fn;
- }
- for (path in routes) {
- fn = routes[path];
- _routes.push(new Route(path, fn));
- }
- };
- return Route = (function() {
- Route.prototype._transformations = [[/:([\w\d]+)/g, '([^/]*)'], [/\*([\w\d]+)/g, '(.*?)']];
- function Route(path, fn) {
- var pattern, replacement, _i, _len, _ref, _ref2;
- this.path = path;
- this.fn = fn;
- _ref = this._transformations;
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- _ref2 = _ref[_i], pattern = _ref2[0], replacement = _ref2[1];
- path = path.replace(pattern, replacement);
- }
- this.pattern = new RegExp("^" + path + "$");
- }
- return Route;
- })();
-})(typeof exports !== "undefined" && exports !== null ? exports : (this['route'] = {}));
View
@@ -8,7 +8,7 @@
"repositories": [{ "type": "git", "url": "https://github.com/amccollum/route.git" }],
"licenses": [{ "type": "MIT", "url": "http://opensource.org/licenses/mit-license.php" }],
- "main": "lib/route.js",
+ "main": "lib/index.js",
"ender": "lib/ender.js",
"directories": { "lib": "lib" },
View
@@ -1,3 +1,4 @@
-(($) ->
- $.ender({ route: require('route') })
-)(ender)
+# (($) ->
+# route = require('route')
+#
+# )(ender)
View
@@ -0,0 +1,42 @@
+route = exports ? (@['route'] = {})
+
+class route.Router
+ _transformations: [
+ [ # Escape URL Special Characters
+ /([?=,\/])/g
+ '\\$1'
+ ],
+
+ [ # Named Parameters
+ /:([\w\d]+)/g
+ '([^/]*)'
+ ],
+
+ [ # Splat Parameters
+ /\*([\w\d]+)/g
+ '(.*?)'
+ ],
+ ]
+
+ constructor: ->
+ @routes = []
+
+ add: (routes) ->
+ for expr, fn of routes
+ pattern = expr
+ for [transformer, replacement] in @_transformations
+ pattern = pattern.replace(transformer, replacement)
+
+ @routes.push({ expr: expr, pattern: new RegExp(pattern), fn: fn })
+
+ return
+
+ run: (path, context) ->
+ results = []
+
+ for route in @routes
+ if (m = route.pattern.exec(path))
+ result = route.fn.apply(context, m.slice(1))
+ results.push(result) if result
+
+ return results
View
@@ -1,66 +0,0 @@
-((route) ->
- _routes = []
- _hash = null
-
- route.init = (run) ->
- onchange = () ->
- hash = $.hash()
- if hash != _hash
- _hash = hash
- route.run(hash)
-
- return
-
- $(window).bind 'hashchange', onchange
- onchange() if run
-
- return
-
- route.navigate = (hash, run) ->
- if not run
- _hash = hash
-
- $.hash(hash)
- return
-
- route.run = (hash) ->
- for route in _routes
- if (m = route.pattern.exec(hash))
- route.fn.apply(route, m.slice(1))
-
- return
-
- route.add = (routes, fn) ->
- if fn
- routes = {}
- routes[routes] = fn
-
- for path, fn of routes
- _routes.push(new Route(path, fn))
-
- return
-
- class Route
- _transformations: [
- [ # Named Parameters
- /:([\w\d]+)/g
- '([^/]*)'
- ],
-
- [ # Splat Parameters
- /\*([\w\d]+)/g
- '(.*?)'
- ],
- ]
-
- constructor: (path, fn) ->
- @path = path
- @fn = fn
-
- for [pattern, replacement] in @_transformations
- path = path.replace(pattern, replacement)
-
- @pattern = new RegExp("^#{path}$")
-
-
-)(exports ? (@['route'] = {}))

0 comments on commit 6d71b2d

Please sign in to comment.