Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update templates/scaffold/controller.ejs #219

Closed
wants to merge 1 commit into from

4 participants

@Eraden

If you are already using function, Model and self can be solved with the use of scopes.

@Eraden Eraden Update templates/scaffold/controller.ejs
If you are already using function, Model and self can be solved with the use of scopes.
db2e1e8
@Techwraith
Owner
@larzconwell

This has a few errors currently, the variable is called <%= names.property.singular %>, but in multiple places you're calling <%= names.constructor.singular %>. The difference is the contructors name is CamelCase while the properties are cameCase.

@Eraden

You are right, i didn't notice that. But I don't have any error in my project.

I suppose that if you charge on line 3 property to constructor problem will be fixed

@larzconwell

I get errors if I do this, just change line 3 to the constructor style, then we'll let @mde decide if we should merge it.

@mde
Owner

Although using a constructor-level 'self' reference reference works in this case, I don't like it as a general approach because it fails if you reuse this object as a prototype for another constructor, or as a mixin. (So this would actually break if someone tried to use this same pattern in their Application controller.)

As a general rule, I much prefer to keep the 'self' reference local to the called method, so you can be absolutely sure that 'self' is the current 'this' that the method is being invoked on.

I think we'll give this one a miss -- but thanks for the pull-request, and I hope you'll keep them coming!

@mde mde closed this
@Eraden
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 10, 2012
  1. @Eraden

    Update templates/scaffold/controller.ejs

    Eraden authored
    If you are already using function, Model and self can be solved with the use of scopes.
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 17 deletions.
  1. +9 −17 templates/scaffold/controller.ejs
View
26 templates/scaffold/controller.ejs
@@ -1,10 +1,11 @@
var <%= names.constructor.plural %> = function () {
+ var self = this,
+ <%= names.property.singular %> = geddy.model.<%= names.constructor.singular %>;
+
this.respondsWith = ['html', 'json', 'xml', 'js', 'txt'];
this.index = function (req, resp, params) {
- var self = this;
-
- geddy.model.<%= names.constructor.singular %>.all(function(err, <%= names.property.plural %>) {
+ <%= names.constructor.singular %>.all(function(err, <%= names.property.plural %>) {
self.respond({params: params, <%= names.property.plural %>: <%= names.property.plural %>});
});
};
@@ -20,8 +21,7 @@ var <%= names.constructor.plural %> = function () {
<% } -%>
<% } -%>
- var self = this
- , <%= names.property.singular %> = geddy.model.<%= names.constructor.singular %>.create(params);
+ var <%= names.property.singular %> = <%= names.constructor.singular %>.create(params);
<%= names.property.singular %>.save(function(err, data) {
if (err) {
@@ -34,25 +34,19 @@ var <%= names.constructor.plural %> = function () {
};
this.show = function (req, resp, params) {
- var self = this;
-
- geddy.model.<%= names.constructor.singular %>.load(params.id, function(err, <%= names.property.singular %>) {
+ <%= names.constructor.singular %>.load(params.id, function(err, <%= names.property.singular %>) {
self.respond({params: params, <%= names.property.singular %>: <%= names.property.singular %>.toObj()});
});
};
this.edit = function (req, resp, params) {
- var self = this;
-
- geddy.model.<%= names.constructor.singular %>.load(params.id, function(err, <%= names.property.singular %>) {
+ <%= names.constructor.singular %>.load(params.id, function(err, <%= names.property.singular %>) {
self.respond({params: params, <%= names.property.singular %>: <%= names.property.singular %>});
});
};
this.update = function (req, resp, params) {
- var self = this;
-
- geddy.model.<%= names.constructor.singular %>.load(params.id, function(err, <%= names.property.singular %>) {
+ <%= names.constructor.singular %>.load(params.id, function(err, <%= names.property.singular %>) {
<%= names.property.singular %>.updateAttributes(params);
<%= names.property.singular %>.save(function(err, data) {
@@ -67,9 +61,7 @@ var <%= names.constructor.plural %> = function () {
};
this.destroy = function (req, resp, params) {
- var self = this;
-
- geddy.model.<%= names.constructor.singular %>.remove(params.id, function(err) {
+ <%= names.constructor.singular %>.remove(params.id, function(err) {
if (err) {
params.errors = err;
self.transfer('edit');
Something went wrong with that request. Please try again.