Skip to content

Instantly share code, notes, and snippets.

@CodeOfficer
Created July 13, 2010 23:07
Show Gist options
  • Save CodeOfficer/474716 to your computer and use it in GitHub Desktop.
Save CodeOfficer/474716 to your computer and use it in GitHub Desktop.
var Comment = Model("comment",
// Class methods
{
persistence: Model.RestPersistence("/posts/:post_id/comments"),
findAllRemote: function(context, callback) {
$.getJSON("/posts/" + context.post_id() + "/comments.json", function(json) {
$.each(json, function(i, comment) {
Comment.add(new Comment($.extend(comment, {post_id: context.post_id()})));
});
callback.call(this);
});
},
create: function(params, callback) {
var comment = new Comment({
post_id: params['post_id'],
name: params['comment']['name'],
email: params['comment']['email'],
gravatar: params['comment']['gravatar'],
body: params['comment']['body']
});
comment.save(function(success) {
if (success) Comment.add(comment);
callback.call(this, success, comment);
});
},
to_html: function() {
var $html = "";
Comment.each(function(i) {
$html += this.to_html();
});
return $html;
}
},
// Instance methods
{
validate: function() {
if (this.attr('body') == null || this.attr('body') == '') {
this.errors.add("body", "cannot be empty");
}
},
body: function() {
return this.attr('body');
},
to_html: function() {
return new EJS({url: "/javascripts/views/comments/_comment.ejs"}).render(this);
},
to_form: function() {
return new EJS({url: "/javascripts/views/comments/_form.ejs"}).render(this);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment