Update URL scheme to include /item/*

* Update the router to understand /item/:name routes in addition to
  v1.0-style parameterized routes
* Add support for updating the current URL based upon the item being
  viewed
* Change LandingPage* to ItemPage*
This commit is contained in:
Andrew Miner
2014-12-28 12:59:29 -08:00
parent 8cde02a10b
commit bf113d26ab
9 changed files with 105 additions and 53 deletions
+52
View File
@@ -0,0 +1,52 @@
###
# Crafting Guide - url_params.coffee
#
# Copyright (c) 2014 by Redwood Labs
# All rights reserved.
###
url = require 'url'
########################################################################################################################
module.exports = class UrlParams
constructor: (parseMap, url=null)->
if not parseMap? then throw new Error 'parseMap is required'
url ?= window.location.href
@_parseMap = {}
for name, options of parseMap
options.type ?= 'string'
options.default ?= null
options.parse = this["_#{options.type}"]
if not options.parse? then throw new Error "#{options.type} is not a valid type"
@_parseMap[name] = options
@parse url
parse: (urlText)->
params = url.parse(urlText, true).query
for name, options of @_parseMap
value = params[name]
if value?
this[name] = options.parse value
else
this[name] = options.default
# Private Methods ##############################################################################
_boolean: (text)->
return true if text in ['true', 'yes']
return false
_integer: (text)->
return null unless text?
result = parseInt text
return null unless _.isNumber result
return result
_string: (text)->
return text