Skip to content

Instantly share code, notes, and snippets.

@epelz
Last active March 14, 2016 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epelz/817c3a346f43b6d7e357 to your computer and use it in GitHub Desktop.
Save epelz/817c3a346f43b6d7e357 to your computer and use it in GitHub Desktop.
Example: Mutation helper
export function heartTask(attr: {
datastore: DatastoreService;
// Thin interfaces for the User/Task model which utilize TypeScript's
// structural typing, but the type aliases are omitted for brevity.
user: UserBase;
task: TaskBase;
}) {
attr.datastore.runInBatch(() => {
// 1. Optimistically create the object representing the heart.
var heartRequest = new HeartCreateRequest({
target: attr.task.dbObjectId(),
user: attr.user.dbObjectId()
});
var heartId = attr.datastore.createDbObject(heartRequest);
// 2. Send server request to update the database.
attr.services.datastore.requestServerChange(
“post”, “/task_heart”, {
// server will create an identical object with the same ID.
global_id: heartId,
task: task.dbObjectId(),
user: attr.user.dbObjectId()
}
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment