From 5748bcde6471b5b79532ea1253d5a1bc2c26cbac Mon Sep 17 00:00:00 2001 From: Andrew Plummer Date: Sun, 26 Feb 2012 00:52:22 +0900 Subject: [PATCH] allow the format to be passed in the connection statement --- demo/index.html | 8 ++++++-- lib/main.js | 32 +++++++++++++++++++------------- test/unit.js | 12 ++++++++++++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/demo/index.html b/demo/index.html index a1c6da0..fdcc85f 100644 --- a/demo/index.html +++ b/demo/index.html @@ -11,11 +11,15 @@ background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#350a0a), to(#180505)); height: 100%; } + + body { + width: 100%; + margin: 0; + } pre { background: none; border: none; padding: 30px; - width: 100%; font-size: 25px; font-family: Courier; outline: none; @@ -143,7 +147,7 @@ api.cors(false); // Allow optional contexts -api.connect('goals/:goal_id/items'); +api.connect('goals/:goal_id/items.json'); api.getItems({ goal_id: 910 }).then(showItems); diff --git a/lib/main.js b/lib/main.js index f511413..3c8e9bd 100644 --- a/lib/main.js +++ b/lib/main.js @@ -50,11 +50,17 @@ return only ? result : actions; } - function getRouteObject(context, route, routingOptions, method) { + function getRouteObject(context, route, routeOptions, method) { var previous, result = context.base.concat(); + route = route.replace(/\.(\w+)$/, function(match, format) { + routeOptions.format = format; + routeOptions.appendFormat = true; + return ''; + }); + arrayEach(route.split('/'), function(str, i, arr) { var fragment, match = str.match(/^:(.+)$/), @@ -70,7 +76,7 @@ // ex. GET /tweets // Both may be allowed for a single method call, but only if "collection" is explicitly // true, allowing for things like uncountable resources that would otherwise have a method collision. - if(!method || method != 'GET' || !routingOptions.collection) { + if(!method || method != 'GET' || !routeOptions.collection) { previous[property] = set; } } @@ -103,7 +109,7 @@ return result; } - function getMethodName(method, routeObject, routingOptions) { + function getMethodName(method, routeObject, routeOptions) { var name = HUMANIZED_HTTP_VERBS[method], reversed, obj; reversed = routeObject.concat().reverse(); arrayEach(reversed, function(el, i, arr) { @@ -134,23 +140,23 @@ Array.prototype.splice.apply(routes1, [startIndex, 0].concat(fragmentsToMerge)); } - function connectRoute(str, routingOptions) { + function connectRoute(str, routeOptions) { var context = this, match, route, method, routeObject, routeParams, as; - routingOptions = routingOptions || {}; - match = str.match(/\s*(get|post|put|delete)?\s*\/?(\S+)\s*(?:(?:params|with)\s+(\S+)\s*)?(?:as\s+(\S+))?/i); - method = match[1] ? match[1].toUpperCase() : 'GET'; - route = match[2]; - routeObject = getRouteObject(context, route, routingOptions, method); - routeParams = getParamsFromString(match[3]) || routingOptions.params; - as = match[4] || routingOptions.as || getMethodName(method, routeObject, routingOptions); + routeOptions = routeOptions || {}; + match = str.match(/\s*(get|post|put|delete)?\s*\/?(\S+)\s*(?:(?:params|with)\s+(\S+)\s*)?(?:as\s+(\S+))?/i); + method = match[1] ? match[1].toUpperCase() : 'GET'; + route = match[2]; + routeObject = getRouteObject(context, route, routeOptions, method); + routeParams = getParamsFromString(match[3]) || routeOptions.params; + as = match[4] || routeOptions.as || getMethodName(method, routeObject, routeOptions); if(context[as]) { // Method exists so merge its route object to allow it a new context. mergeRoutes(context[as].routeObject, routeObject); } else { - context[as] = function(params, localOptions) { + context[as] = function(params, options) { var url, key; if(typeof params == 'string') { @@ -165,7 +171,7 @@ } params = $.extend({}, context.defaultParams, routeParams, params); - options = $.extend({}, context.defaultOptions, localOptions); + options = $.extend({}, context.defaultOptions, routeOptions, options); url = resolveURL(context, routeObject, params, options); diff --git a/test/unit.js b/test/unit.js index 4944a62..6ccfec1 100644 --- a/test/unit.js +++ b/test/unit.js @@ -1147,5 +1147,17 @@ assertRouteCalled(api, 'http://domain/13', 'GET') }); + test('route allows format', function() { + api.connect('GET /items.json'); + api.getItems(); + assertRouteCalled(api, 'http://domain/items.json', 'GET') + }); + + test('route allows format with fragment', function() { + api.connect('GET /:name.json'); + api.getName({ name: 'jackson' }); + assertRouteCalled(api, 'http://domain/jackson.json', 'GET') + }); + })();