app/assets/javascripts/joosy/core/router.js.coffee in joosy-1.0.0.RC7 vs app/assets/javascripts/joosy/core/router.js.coffee in joosy-1.1.0.alpha.1

- old
+ new

@@ -24,18 +24,24 @@ # # 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) -> @@ -43,10 +49,21 @@ # Clears the routes # reset: -> @rawRoutes = Object.extended() @routes = Object.extended() + @__namespace = "" + @__asNamespace = "" + + + # + # Draws the routes similar to Ruby on Rails + # + # @param [Function] block callback for child commands + # + draw: (block)-> + block.call(this) if Object.isFunction(block) # # Registers the set of raw routes # This method will only store routes and will not make them act immediately # Routes get registered only once at system initialization during #__setupRoutes call @@ -76,12 +93,73 @@ 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) + # + # @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() + else + as = options["as"] + + routeName = @__namespace + route + map = {} + 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) + + # + # Routes the 404 + # + # @param options [String] to function to which the route routes + # + notFound: (options={}) -> + @match(404, to: options["to"]) + + # + # Namespaces a match route + # + # @param [String] name name of the namespace, prefixes other commands + # + # @option [Hash] options "as", prefixes all other "as" commands + # @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"] + 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 # __setupRoutes: -> @@ -197,7 +275,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] 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}#{fnc(options)}" Joosy.Module.merge Joosy.Router, Joosy.Modules.Events