lib/routes.js.coffee in js-routes-0.9.0 vs lib/routes.js.coffee in js-routes-0.9.1
- old
+ new
@@ -3,31 +3,42 @@
defaults =
prefix: "PREFIX"
default_url_options: DEFAULT_URL_OPTIONS
NodeTypes = NODE_TYPES
+
Utils =
- serialize: (obj) ->
- return "" unless obj
+ serialize: (object, prefix = null) ->
+ return "" unless object
+ if !prefix and !(@get_object_type(object) is "object")
+ throw new Error("Url parameters should be a javascript hash")
+
if window.jQuery
- result = window.jQuery.param(obj)
+ result = window.jQuery.param(object)
return (if not result then "" else result)
+
s = []
- for own key, prop of obj when prop?
- if @getObjectType(prop) is "array"
- for val, i in prop
- s.push "#{key}#{encodeURIComponent("[]")}=#{encodeURIComponent(val.toString())}"
+ switch @get_object_type(object)
+ when "array"
+ for element, i in object
+ s.push @serialize(element, prefix + "[]")
+ when "object"
+ for own key, prop of object when prop?
+ key = "#{prefix}[#{key}]" if prefix?
+ s.push @serialize(prop, key)
else
- s.push "#{key}=#{encodeURIComponent(prop.toString())}"
+ if object
+ s.push "#{encodeURIComponent(prefix.toString())}=#{encodeURIComponent(object.toString())}"
+
return "" unless s.length
s.join("&")
clean_path: (path) ->
path = path.split("://")
last_index = path.length - 1
- path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/\/$/m, "")
+ path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/.\/$/m, "")
path.join "://"
set_default_url_options: (optional_parts, options) ->
for part, i in optional_parts
if not options.hasOwnProperty(part) and defaults.default_url_options.hasOwnProperty(part)
@@ -40,26 +51,26 @@
delete options.anchor
anchor
extract_options: (number_of_params, args) ->
ret_value = {}
- if args.length > number_of_params and @getObjectType(args[args.length - 1]) is "object"
+ if args.length > number_of_params
ret_value = args.pop()
ret_value
path_identifier: (object) ->
return "0" if object is 0
# null, undefined, false or ''
return "" unless object
property = object
- if @getObjectType(object) is "object"
+ if @get_object_type(object) is "object"
property = object.to_param or object.id or object
- property = property.call(object) if @getObjectType(property) is "function"
+ property = property.call(object) if @get_object_type(property) is "function"
property.toString()
clone: (obj) ->
- return obj if !obj? or "object" isnt @getObjectType(obj)
+ return obj if !obj? or "object" isnt @get_object_type(obj)
copy = obj.constructor()
copy[key] = attr for own key, attr of obj
copy
prepare_parameters: (required_parameters, actual_parameters, options) ->
@@ -68,11 +79,13 @@
result
build_path: (required_parameters, optional_parts, route, args) ->
args = Array::slice.call(args)
opts = @extract_options(required_parameters.length, args)
- throw new Error("Too many parameters provided for path") if args.length > required_parameters.length
+
+ if args.length > required_parameters.length
+ throw new Error("Too many parameters provided for path")
parameters = @prepare_parameters(required_parameters, args, opts)
@set_default_url_options optional_parts, parameters
result = "#{@get_prefix()}#{@visit(route, parameters)}"
url = Utils.clean_path("#{result}#{@extract_anchor(parameters)}")
url += "?#{url_params}" if (url_params = @serialize(parameters)).length
@@ -124,11 +137,11 @@
#
visit_globbing: (route, parameters, optional) ->
[type, left, right] = route
value = parameters[left]
return @visit(route, parameters, optional) unless value?
- parameters[left] = switch @getObjectType(value)
+ parameters[left] = switch @get_object_type(value)
when "array"
value.join("/")
else
value
@visit route, parameters, optional
@@ -171,10 +184,10 @@
return @_classToTypeCache if @_classToTypeCache?
@_classToTypeCache = {}
for name in "Boolean Number String Function Array Date RegExp Undefined Null".split(" ")
@_classToTypeCache["[object " + name + "]"] = name.toLowerCase()
@_classToTypeCache
- getObjectType: (obj) ->
+ get_object_type: (obj) ->
return window.jQuery.type(obj) if window.jQuery and window.jQuery.type?
strType = Object::toString.call(obj)
@_classToType()[strType] or "object"
namespace: (root, namespaceString) ->