Skip to content

Commit

Permalink
Added support for create and edit routes and templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed May 4, 2010
1 parent ec493e8 commit 98475f4
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 59 deletions.
8 changes: 6 additions & 2 deletions geddy-core/lib/app.js
Expand Up @@ -33,7 +33,9 @@ var App = function (initData) {

this.run = function (req, resp) {
var url = req.url;
var base = fleegix.url.getBase(url);
// Split only on question mark -- using semicolon delimiter for
// edit-flag hack in resource-routes
var base = url.split(/\?|=/)[0];
var route = router.parse(base, req.method);

try {
Expand All @@ -49,7 +51,9 @@ var App = function (initData) {

sess.init(function () {

var qs = fleegix.url.getQS(url);
// Split only on question mark -- using semicolon delimiter for
// edit-flag hack in resource-routes
var qs = url.split('?')[1] || '';
var qsParams = fleegix.url.qsToObject(qs);
var params = util.meta.mixin({}, route.params);
var params = util.meta.mixin(params, qsParams);
Expand Down
13 changes: 12 additions & 1 deletion geddy-core/lib/controller.js
Expand Up @@ -139,7 +139,18 @@ Controller.prototype = new function () {
}
};

this.redirect = function (url) {
this.redirect = function (redir) {
var url;
if (typeof redir == 'string') {
url = redir;
}
else {
var contr = redir.controller || this.name;
var act = redir.action;
var ext = redir.extension || this.params.extension;
contr = util.string.decamelize(contr);
url = '/' + contr + '/' + act + '.' + ext;
}
var r = new response.Response(this.response);
var headers = {'Location': url};
headers['Set-Cookie'] = this.cookies.serialize();
Expand Down
14 changes: 7 additions & 7 deletions geddy-core/lib/router.js
Expand Up @@ -16,7 +16,6 @@
*
*/

var fleegix = require('geddy-core/lib/fleegix');
var sys = require('sys');

/*
Expand All @@ -32,7 +31,7 @@ var sys = require('sys');
var Router = function () {
// From BomberJS: http://bomber.obtdev.com/
const KEY_PATTERN = /:([a-zA-Z_][a-zA-Z0-9_]*)/g;

This comment has been minimized.

Copy link
@dandean

dandean May 28, 2010

Can this pattern be simplified to:

/:([a-zA-Z_]\w*)/g

This comment has been minimized.

Copy link
@mde

mde May 29, 2010

Author Contributor

Looks like it, yes. Nice simplification.

This comment has been minimized.

Copy link
@dandean

dandean May 29, 2010

Killer.

const MATCH_PATTERN_STRING = "([^\\\/.]+)";
const MATCH_PATTERN_STRING = "([^\\\/.;]+)";
var _routes = [];
var _namedRoutes = {};
var _regExpEscape = function(str) {
Expand All @@ -44,6 +43,7 @@ var Router = function () {
{method: 'get', path: '/add', action: 'add'},
{method: 'post', path: null, action: 'create'},
{method: 'get', path: '/:id', action: 'show'},
{method: 'get', path: '/:id', action: 'edit', specialFlag: true},
{method: 'put', path: '/:id', action: 'update'},
{method: 'delete', path: '/:id', action: 'remove'},
];
Expand Down Expand Up @@ -72,15 +72,14 @@ var Router = function () {

this.resource = function (resource) {
var controllerName;
// Convert underscores to camelCase, e.g., 'neilPearts'
controllerName = fleegix.string.camelize(resource);
// Capitalize the first letter, e.g., 'NeilPearts'
controllerName = fleegix.string.capitalize(controllerName);
// Convert underscores to CamelCase with initial cap, e.g., 'NeilPearts'
controllerName = util.string.camelize(resource, true);
// Add a resource-based route for each type
var r;
for (var i = 0; i < _resourceTypes.length; i++) {
var r = _resourceTypes[i];
this.match('/' + resource + (r.path || '') + '.:extension', r.method).to(
var editFlag = r.specialFlag ? ';:__editflag__' : '';
this.match('/' + resource + (r.path || '') + '.:extension' + editFlag, r.method).to(
{ controller: controllerName, action: r.action});
}
};
Expand All @@ -98,6 +97,7 @@ var Router = function () {
var key = route.keys[j];
route.params[key] = match[j];
}
delete route.params.__editflag__;
route.params.controller = route.controller;
route.params.action = route.action;
return route;
Expand Down
7 changes: 6 additions & 1 deletion geddy-core/scripts/gen/resource_controller.ejs
Expand Up @@ -11,13 +11,18 @@ var <%= namePlural %> = function () {
};

this.create = function (params) {
this.respond({params: params});
// Save the resource, then redirect to index
this.redirect({action: 'index'});
};

this.show = function (params) {
this.respond({params: params});
};

this.edit = function (params) {
this.respond({params: params});
};

this.update = function (params) {
this.respond({params: params});
};
Expand Down
16 changes: 0 additions & 16 deletions geddy-core/scripts/gen/views/add.html.ejs
@@ -1,19 +1,3 @@
<!--
Geddy JavaScript Web development framework
Copyright 2112 Matthew Eernisse (mde@fleegix.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<body>
<h3>Params</h3>
Expand Down
11 changes: 11 additions & 0 deletions geddy-core/scripts/gen/views/edit.html.ejs
@@ -0,0 +1,11 @@
<html>
<body>
<h3>Params</h3>
<ul>
<% for (var p in params) { %>
<li><%= p + ': ' + params[p]; %></li>
<% } %>
</ul>
</body>
</html>

16 changes: 0 additions & 16 deletions geddy-core/scripts/gen/views/index.html.ejs
@@ -1,19 +1,3 @@
<!--
Geddy JavaScript Web development framework
Copyright 2112 Matthew Eernisse (mde@fleegix.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<body>
<h3>Params</h3>
Expand Down
16 changes: 0 additions & 16 deletions geddy-core/scripts/gen/views/show.html.ejs
@@ -1,19 +1,3 @@
<!--
Geddy JavaScript Web development framework
Copyright 2112 Matthew Eernisse (mde@fleegix.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<body>
<h3>Params</h3>
Expand Down

0 comments on commit 98475f4

Please sign in to comment.