Permalink
Browse files

Added support for create/update routing, for _method param override.

  • Loading branch information...
1 parent 98475f4 commit 2b96485f368e05e2e6040f8d7b17a71d53d22af9 @mde mde committed May 5, 2010
View
@@ -153,6 +153,9 @@ POST */snow_dogs[.extension]<br/>
GET */snow_dogs/:id[.extension]<br/>
(SnowDogs controller, show action)
+GET */snow_dogs/:id[.extension];edit<br/>
+(SnowDogs controller, edit action)
+
PUT */snow_dogs/:id[.extension]<br/>
(SnowDogs controller, update action)
View
@@ -36,7 +36,11 @@ var App = function (initData) {
// 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);
+ var qs = url.split('?')[1] || '';
+ var qsParams = fleegix.url.qsToObject(qs);
+ var method = (req.method == 'POST' && qsParams._method) ?
+ qsParams._method : req.method;
+ var route = router.parse(base, method);
try {
// If the route is a match, run the matching controller/action
@@ -53,10 +57,9 @@ var App = function (initData) {
// 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);
+ var params;
+ params = util.meta.mixin({}, route.params);
+ params = util.meta.mixin(params, qsParams);
// Instantiate the matching controller from the registry
var constructor = controllerRegistry[route.controller];
@@ -47,7 +47,7 @@ var Controller = function (obj) {
this.respondsWith = ['text'];
// The name, in lowercase_with_underscores, used for
// picking the template if any to attempt to render
- this.nameDeCamelized = null;
+ this.nameDecamelized = null;
// Content to respond with -- can be an Object or String
this.content = '';
// High-level set of options which can represent multiple
@@ -71,7 +71,7 @@ var Controller = function (obj) {
this[p] = obj[p];
}
- this.nameDeCamelized = fleegix.string.deCamelize(this.name);
+ this.nameDecamelized = util.string.decamelize(this.name);
};
@@ -148,8 +148,12 @@ Controller.prototype = new function () {
var contr = redir.controller || this.name;
var act = redir.action;
var ext = redir.extension || this.params.extension;
+ var id = redir.id;
contr = util.string.decamelize(contr);
- url = '/' + contr + '/' + act + '.' + ext;
+ url = '/' + contr;
+ url += act ? '/' + act : '';
+ url += id ? '/' + id : '';
+ url += '.' + ext;
}
var r = new response.Response(this.response);
var headers = {'Location': url};
@@ -346,7 +350,7 @@ Controller.prototype = new function () {
this.formatContent = function (format, content) {
if (format == 'html') {
var _this = this;
- var name = this.nameDeCamelized;
+ var name = this.nameDecamelized;
var act = this.params.action;
// E.g., app/views/snow_dogs/index.html.ejs
var path = 'app/views/' + name + '/' + act + '.html.ejs';
View
@@ -100,6 +100,7 @@ var Router = function () {
delete route.params.__editflag__;
route.params.controller = route.controller;
route.params.action = route.action;
+ route.params.method = method;
return route;
}
}
@@ -77,7 +77,7 @@ exports.tasks = {
// TODO: No fancy pluralization yet
var namePlural = names[1] || nameSingular + 's';
- // Convert underscores to camelCase, e.g., 'neilPeart'
+ // Convert underscores to camelCase, e.g., 'NeilPeart'
var nameSingularConverted = util.string.camelize(nameSingular, true);
var namePluralConverted = util.string.camelize(namePlural, true);
@@ -11,8 +11,8 @@ var <%= namePlural %> = function () {
};
this.create = function (params) {
- // Save the resource, then redirect to index
- this.redirect({action: 'index'});
+ // Save the resource, then display index page
+ this.redirect({controller: this.name});
};
this.show = function (params) {
@@ -24,7 +24,8 @@ var <%= namePlural %> = function () {
};
this.update = function (params) {
- this.respond({params: params});
+ // Save the resource, then display the item page
+ this.redirect({controller: this.name, id: params.id});
};
this.remove = function (params) {

0 comments on commit 2b96485

Please sign in to comment.