Beginning of a generic resource router.
authorDavid Schoonover <dsc@wikimedia.org>
Mon, 16 Jul 2012 21:41:31 +0000 (14:41 -0700)
committerDavid Schoonover <dsc@wikimedia.org>
Mon, 16 Jul 2012 21:41:31 +0000 (14:41 -0700)
src/base/resource-router.co [new file with mode: 0644]

diff --git a/src/base/resource-router.co b/src/base/resource-router.co
new file mode 100644 (file)
index 0000000..f5ce9da
--- /dev/null
@@ -0,0 +1,52 @@
+Backbone = require 'backbone'
+
+{ _, op,
+} = require 'limn/util'
+{ BaseBackboneMixin, mixinBase,
+} = require 'limn/base/base-mixin'
+
+
+
+
+
+ResourceRouter = exports.ResourceRouter = Backbone.Router.extend mixinBase do
+    __bind__ : []
+    
+    /**
+     * Singular, lowercase resource-noun.
+     * @optional
+     * @type String
+     * @example "user"
+     */
+    id : null
+    
+    /**
+     * Plural, lowercase resource-noun.
+     * @required
+     * @type String
+     * @example "users"
+     */
+    name : null
+    
+    
+    
+    constructor : function ResourceRouter (opts={})
+        @__class__      = @constructor
+        @__superclass__ = @..__super__.constructor
+        @waitingOn      = 0
+        opts.routes or= @makeRoutes()
+        Backbone.Router.apply this, opts
+    
+    
+    makeRoutes: ->
+        {name, id} = this
+        routes = {}
+        # XXX: reverse these mappings?
+        routes["#name/(new|edit)"] = @create   if typeof @create   is 'function'
+        routes["#name/:#id/edit"]  = @edit     if typeof @edit     is 'function'
+        routes["#name/:#id"]       = @show     if typeof @show     is 'function'
+        routes["#name"]            = @index    if typeof @index    is 'function'
+    
+    
+    
+