Skip to content

Instantly share code, notes, and snippets.

@ratnikov
Created February 18, 2009 19:47
Show Gist options
  • Save ratnikov/66501 to your computer and use it in GitHub Desktop.
Save ratnikov/66501 to your computer and use it in GitHub Desktop.
var GroupStory = function() {
return {
build: function(spec) {
var private = {};
if (typeof(spec.group_id) !== "undefined") {
private.group_id = spec.group_id;
} else {
throw "group_id property missing in spec";
}
var self = {
attributes: { },
view: function() {
return "<div class='group-story'>"+self.attributes.body+"</div>";
}
};
AjaxResource.Base.extend(self, {
resource_name: 'group_story',
singular_path: '/story',
plural_path: '/stories',
prefix: '/user/groups/'+private.group_id
});
return self;
}
};
}();
GroupStory.Errors = function() {
var private = { };
jQuery(document).ready(function() {
private.error_div = jQuery('div.error');
private.ul = private.error_div.find("ul");
});
var self = {
clear: function() {
private.ul.html("");
},
append: function(errors) {
jQuery.each(errors, function() {
// assuming the errors are in the [ desc, message ] format
if (this.length == 2) {
var desc = this[0];
var msg = this[1];
} else {
throw "GroupStory.Errors: Unsupported error format";
}
var full_message;
if (desc !== 'base') {
full_message = desc+" "+msg;
} else {
// if it's a base error, add just the error message
full_message = msg;
}
private.ul.append("<li>"+full_message+"</li>");
});
},
set: function(errors) {
self.clear();
self.append(errors);
self.show();
},
hide: function() { private.error_div.hide(); },
show: function() { private.error_div.show(); }
};
return self;
}();
GroupStory.List = function() {
var private = { };
jQuery(document).ready(function() {
private.list = jQuery("ul.group-story-list");
});
var self = {
prepend: function(story) {
var tag_head = "<li";
if (typeof(self.li_class) !== "undefined") {
tag_head += " class='"+self.li_class+"'";
} else {
// if no class is specified, none is added
}
private.list.prepend(tag_head+story.view()+"</li>");
}
};
return self;
}();
jQuery(document).ready(function() {
var div = jQuery('div.group-story-new');
jQuery(div).each(function() {
var group_id = jQuery(this).attr("group_id");
if (typeof(group_id) === "undefined") {
throw "Need group id specified";
}
var story = GroupStory.build({ group_id: group_id });
var button = jQuery(div).find(':submit');
var creation_semaphore = AjaxResource.Semaphore.init({
on_available: function() { button.attr("disabled", false); },
on_restricted: function() { button.attr("disabled", true); }
});
jQuery(div).find(':submit').click(function() {
// allow ajax create request only for when no requests are pending
if (creation_semaphore.available()) {
story.parse_fields(div);
story.create(function(updated_story) {
if (updated_story !== null) {
if (updated_story.has_errors()) {
GroupStory.Errors.set(updated_story.errors);
} else {
GroupStory.Errors.hide();
GroupStory.List.prepend(story);
}
} else {
throw "Failed to parse response";
}
// request handled, decrease the semaphore
creation_semaphore.dec();
});
creation_semaphore.inc();
} else {
// do nothing since already sending post
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment