Skip to content

Instantly share code, notes, and snippets.

@jamiefolsom
Created September 5, 2012 17:24
Show Gist options
  • Save jamiefolsom/3640483 to your computer and use it in GitHub Desktop.
Save jamiefolsom/3640483 to your computer and use it in GitHub Desktop.
Adding events to the Store plugin
# Public: Callback method for annotationCreated event. Receives an annotation
# and sends a POST request to the sever using the URI for the "create" action.
#
# annotation - An annotation Object that was created.
#
# Examples
#
# store.annotationCreated({text: "my new annotation comment"})
# # => Results in an HTTP POST request to the server containing the
# # annotation as serialised JSON.
#
# Returns nothing.
annotationCreated: (annotation) ->
# Pre-register the annotation so as to save the list of highlight
# elements.
if annotation not in @annotations
this.registerAnnotation(annotation)
this._apiRequest('create', annotation, (data) =>
# Update with (e.g.) ID from server.
if not data.id?
console.warn Annotator._t("Warning: No ID returned from server for annotation "), annotation
this.updateAnnotation annotation, data
)
else
# This is called to update annotations created at load time with
# the highlight elements created by Annotator.
this.updateAnnotation annotation, {}
this.publish('annotationStored', [annotation])
# Public: Callback method for annotationUpdated event. Receives an annotation
# and sends a PUT request to the sever using the URI for the "update" action.
#
# annotation - An annotation Object that was updated.
#
# Examples
#
# store.annotationUpdated({id: "blah", text: "updated annotation comment"})
# # => Results in an HTTP PUT request to the server containing the
# # annotation as serialised JSON.
#
# Returns nothing.
annotationUpdated: (annotation) ->
if annotation in this.annotations
this._apiRequest 'update', annotation, ((data) => this.updateAnnotation(annotation, data))
this.publish('annotationStored', [annotation])
# Public: Callback method for annotationDeleted event. Receives an annotation
# and sends a DELETE request to the server using the URI for the destroy
# action.
#
# annotation - An annotation Object that was deleted.
#
# Examples
#
# store.annotationDeleted({text: "my new annotation comment"})
# # => Results in an HTTP DELETE request to the server.
#
# Returns nothing.
annotationDeleted: (annotation) ->
if annotation in this.annotations
this._apiRequest 'destroy', annotation, (() => this.unregisterAnnotation(annotation))
this.publish('annotationDestroyed', [annotation])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment