Skip to content

Commit

Permalink
allow the format to be passed in the connection statement
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewplummer committed Feb 25, 2012
1 parent ebe296a commit 5748bcd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
8 changes: 6 additions & 2 deletions demo/index.html
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
</pre>
Expand Down
32 changes: 19 additions & 13 deletions lib/main.js
Expand Up @@ -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(/^:(.+)$/),
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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') {
Expand All @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions test/unit.js
Expand Up @@ -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')
});


})();

0 comments on commit 5748bcd

Please sign in to comment.