Skip to content

Instantly share code, notes, and snippets.

@bobtfish
Created November 28, 2009 13:46
Show Gist options
  • Select an option

  • Save bobtfish/244511 to your computer and use it in GitHub Desktop.

Select an option

Save bobtfish/244511 to your computer and use it in GitHub Desktop.
// Roundtripping objects in Javascript with Joose:
Joose.Storage.Unpacker.patchJSON();
// Represents a page fragment
Class("Fragment", {
does: Joose.Storage,
has: {
html: {
is: "rw",
init: "&nbsp"
},
id: {
is: "rw"
},
after_fragment: {
is: "rw"
}
},
});
// Represents a response to sending a page fragment to the server..
Class("SaveResponse", {
does: Joose.Storage,
has: {
id: {
is: "ro"
},
old_id: {
is: "ro",
predicate: "has_old_id"
},
status: {
is: "ro"
}
},
methods: {
forPanel: function () {
if (this.status == 'DELETED') {
return "<div>Deleted <b>" + this.id + "</b></div>";
}
else if (this.status == 'OK') {
return "<div>Saved <b>" + this.id + "</b></div>";
}
return JSON.stringify(this);
}
}
});
// Actual code to deal with the command
$.ajax({
type: "POST",
//url: "bin/process.php",
data: JSON.stringify(myFragment),
contentType: "application/json",
success: function(json) {
var response = JSON.parse(json);
// Assign status from response.forPanel() to UI element here..
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment