app/assets/javascripts/joosy/core/router.js.coffee in joosy-1.1.0.alpha.3 vs app/assets/javascripts/joosy/core/router.js.coffee in joosy-1.1.0

- old
+ new

@@ -24,23 +24,23 @@ # # Flattern routes mapped to regexps (to check if current route is what we # need) and actual executors # routes: Object.extended() - + # # The regexp to restrict the next loading url. By default set to false and # therefore no restrictions apply. # restrictPattern: false - + # # TODO: Write readme # __namespace: "" __asNamespace: "" - + # # Set the restriction pattern. If the requested url does not match this it # will not load. Set `false` to avoid check. # restrict: (@restrictPattern) -> @@ -51,12 +51,12 @@ reset: -> @rawRoutes = Object.extended() @routes = Object.extended() @__namespace = "" @__asNamespace = "" - - + + # # Draws the routes similar to Ruby on Rails # # @param [Function] block callback for child commands # @@ -93,53 +93,53 @@ setTimeout => @__ignoreRequest = false , 2 # jQuery.hashchange checks hash changing every 1ms else history[if options.replaceState then 'replaceState' else 'pushState'] {}, '', '#'+path - + # - # Match route (ads it to @rawRoutes) + # Match route (adds it to @rawRoutes) # # @param [String] route similar to ones sent in map hash - # + # # @param options [String] to function to which the route routes # @option options [String] as name of the route, used for reverse routing # match: (route, options={}) -> if @__asNamespace - as = @__asNamespace + options["as"].capitalize() + as = @__asNamespace + options.as.capitalize() else - as = options["as"] - + as = options.as + routeName = @__namespace + route map = {} - map[route] = options["to"] - + map[route] = options.to + Joosy.Module.merge @rawRoutes, map - + @__injectReverseUrl(as, routeName) - + # # Shortcut to match "/" # # @param options [String] to function to which the route routes # @option options [String] as name of the route, used for reverse routing # default it is "root" # root: (options={}) -> - as = options["as"] || "root" - @match("/", to: options["to"], as: as) - + as = options.as || "root" + @match("/", to: options.to, as: as) + # # Routes the 404 # # @param options [String] to function to which the route routes # notFound: (options={}) -> - @match(404, to: options["to"]) - + @match(404, to: options.to) + # # Namespaces a match route # # @param [String] name name of the namespace, prefixes other commands # @@ -147,18 +147,20 @@ # @param [Function] block callback for child commands namespace: (name, options={}, block) -> if Object.isFunction(options) block = options options = {} - + newScope = $.extend({}, this) newScope.rawRoutes = {} newScope.__namespace += name - newScope.__asNamespace += "#{options["as"]}" if options["as"] + if options.as + options.as = options.as.capitalize() if newScope.__asNamespace.length > 0 + newScope.__asNamespace += options.as block.call(newScope) if Object.isFunction(block) @rawRoutes[name] = newScope.rawRoutes - + # # Inits the routing system and loads the current route # Binds the window hashchange event and therefore should only be called once # during system startup # @@ -275,33 +277,33 @@ unless @isBlank() pair = @split '=' params[pair[0]] = pair[1] params - + # # Injects reverse routing function into global namespace - # @param [String] as The name for the route, ex: for "projects" - # builds "projects_url" and "projects_path" functions + # @param [String] as The name for the route, ex: for "projects" + # builds "projectsUrl" and "projectsPath" functions # @param [String] route Entire route, joined by namespaces, ex: # "/projects/": # "/:id" : # "/edit": TestPage # joins to "/projects/:id/edit" # __injectReverseUrl: (as, route) -> return if as == undefined - + fnc = (options) -> url = route (route.match(/\/:[^\/]+/g) || []).each (str) -> url = url.replace(str.substr(1), options[str.substr(2)]) "#!#{url}" Joosy.Helpers.Application["#{as}Path"] = (options) -> fnc(options) - + Joosy.Helpers.Application["#{as}Url"] = (options) -> - url = 'http://' + window.location.host + window.location.pathname + url = window.location.protocol + '//' + window.location.host + window.location.pathname "#{url}#{fnc(options)}" Joosy.Module.merge Joosy.Router, Joosy.Modules.Events