Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
94 lines (77 sloc) 2.81 KB
var <%= names.constructor.plural %> = function () {
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 %>) {
self.respond({params: params, <%= names.property.plural %>: <%= names.property.plural %>});
});
};
<% if(!bare) { %>
this.add = function (req, resp, params) {
this.respond({params: params});
};
this.create = function (req, resp, params) {
<% for(var i in properties) { -%>
<% if(properties[i].name === 'id' && properties[i].type === 'string') { -%>
params.id = params.id || geddy.string.uuid(10);
<% } -%>
<% } -%>
var self = this
, <%= names.property.singular %> = geddy.model.<%= names.constructor.singular %>.create(params);
if (!<%= names.property.singular %>.isValid()) {
params.errors = <%= names.property.singular %>.errors;
self.transfer('add');
}
<%= names.property.singular %>.save(function(err, data) {
if (err) {
params.errors = err;
self.transfer('add');
} else {
self.redirect({controller: self.name});
}
});
};
this.show = function (req, resp, params) {
var self = this;
geddy.model.<%= names.constructor.singular %>.first(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 %>.first(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 %>.first(params.id, function(err, <%= names.property.singular %>) {
<%= names.property.singular %>.updateAttributes(params);
if (!<%= names.property.singular %>.isValid()) {
params.errors = <%= names.property.singular %>.errors;
self.transfer('edit');
}
<%= names.property.singular %>.save(function(err, data) {
if (err) {
params.errors = err;
self.transfer('edit');
} else {
self.redirect({controller: self.name});
}
});
});
};
this.destroy = function (req, resp, params) {
var self = this;
geddy.model.<%= names.constructor.singular %>.remove(params.id, function(err) {
if (err) {
params.errors = err;
self.transfer('edit');
} else {
self.redirect({controller: self.name});
}
});
};
<% } %>
};
exports.<%= names.constructor.plural %> = <%= names.constructor.plural %>;
Jump to Line
Something went wrong with that request. Please try again.