Skip to content

Instantly share code, notes, and snippets.

@shaneriley
Created May 20, 2011 00:22
Show Gist options
  • Save shaneriley/982086 to your computer and use it in GitHub Desktop.
Save shaneriley/982086 to your computer and use it in GitHub Desktop.
Object-Oriented jQuery!!!!!1
$(function() {
var app = {
tasks: {
selector: "section > ul > li"
}
}
app.tasks.heading = {
selector: app.tasks.selector + " h2",
click: function() {
$(this).closest("li").find(".details").toggle();
}
};
app.actions = {
selector: app.tasks.selector + " fieldset.actions",
parent: app.tasks.selector + " .details",
save: {
selector: ":submit",
click: function() {
$(this).closest(app.actions.parent).hide();
return false;
}
},
"delete": {
selector: "button[name=delete]",
click: function() {
$(this).closest("li").remove();
return false;
}
},
complete: {
selector: "button[name=complete]",
click: function() {
$(this).closest("li").addClass("completed");
return false;
}
}
};
attachEvents(app);
function attachEvents(obj) {
var v;
if ("selector" in obj) {
for (v in obj) {
if (obj[v].constructor === Object) {
attachEvents(obj[v]);
}
if (v in $.attrFn) {
$(obj.selector)[v](obj[v]);
}
}
}
else {
for (v in obj) {
if (obj[v].constructor === Object) {
attachEvents(obj[v]);
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment